|
| 1 | +import 'cross-fetch/polyfill' |
| 2 | + |
| 3 | +import { InitOptions, Options, Headers, AuthBody } from './types' |
| 4 | +import { removeLeadingSlash } from './utils' |
| 5 | + |
| 6 | +export class createClient { |
| 7 | + private client_id: string |
| 8 | + private client_secret?: string |
| 9 | + private options?: Options |
| 10 | + |
| 11 | + constructor(options: InitOptions) { |
| 12 | + const { client_id, client_secret, ...others } = options |
| 13 | + |
| 14 | + this.client_id = client_id |
| 15 | + this.client_secret = client_secret ? client_secret : undefined |
| 16 | + this.options = others || {} |
| 17 | + } |
| 18 | + |
| 19 | + async request(method: string, path: string, data: object = undefined) { |
| 20 | + try { |
| 21 | + const { application, currency, customer_token } = this.options |
| 22 | + const uri: string = `https://api.moltin.com/v2/${removeLeadingSlash( |
| 23 | + path |
| 24 | + )}` |
| 25 | + const headers: Headers = { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + Authorization: `Bearer ${await this.authenticate()}`, |
| 28 | + ...(application && { 'X-MOLTIN-APPLICATION': application }), |
| 29 | + ...(currency && { 'X-MOLTIN-CURRENCY': currency }), |
| 30 | + ...(customer_token && { 'X-MOLTIN-CUSTOMER-TOKEN': customer_token }) |
| 31 | + } |
| 32 | + |
| 33 | + const response = await fetch(uri, { |
| 34 | + method, |
| 35 | + headers, |
| 36 | + ...(data && { body: JSON.stringify({ data }) }) |
| 37 | + }) |
| 38 | + const json = await response.json() |
| 39 | + |
| 40 | + if (response.ok) { |
| 41 | + return json |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + errors: json.errors |
| 46 | + } |
| 47 | + } catch (errors) { |
| 48 | + return { |
| 49 | + errors |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + async authenticate() { |
| 55 | + const { client_id, client_secret } = this |
| 56 | + |
| 57 | + if (!client_id) { |
| 58 | + throw new Error('You must provide a client_id') |
| 59 | + } |
| 60 | + |
| 61 | + const uri: string = 'https://api.moltin.com/oauth/access_token' |
| 62 | + const body: AuthBody = { |
| 63 | + grant_type: client_secret ? 'client_credentials' : 'implicit', |
| 64 | + client_id, |
| 65 | + ...(client_secret && { client_secret }) |
| 66 | + } |
| 67 | + |
| 68 | + const response = await fetch(uri, { |
| 69 | + method: 'POST', |
| 70 | + headers: { |
| 71 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 72 | + }, |
| 73 | + body: Object.keys(body) |
| 74 | + .map(k => `${encodeURIComponent(k)}=${encodeURIComponent(body[k])}`) |
| 75 | + .join('&') |
| 76 | + }) |
| 77 | + |
| 78 | + const { access_token }: { access_token: string } = await response.json() |
| 79 | + |
| 80 | + if (!access_token) { |
| 81 | + throw new Error('Unable to obtain an access token') |
| 82 | + } |
| 83 | + |
| 84 | + return access_token |
| 85 | + } |
| 86 | + |
| 87 | + post(path: string, data: object) { |
| 88 | + return this.request('POST', path, data) |
| 89 | + } |
| 90 | + |
| 91 | + get(path: string) { |
| 92 | + return this.request('GET', path) |
| 93 | + } |
| 94 | + |
| 95 | + put(path: string, data: object) { |
| 96 | + return this.request('PUT', path, data) |
| 97 | + } |
| 98 | + |
| 99 | + delete(path: string) { |
| 100 | + return this.request('DELETE', path) |
| 101 | + } |
| 102 | +} |
0 commit comments