import fetch from 'node-fetch' import { Parameter, Endpoint } from './types' import { makeSystemParameters } from './signature' import { xmlize } from './xml' import FormData from 'form-data' export class Lazada { endpoint: string appKey: string appSecret: string accessToken: string constructor(countryCode: string, appKey: string, appSecret: string) { this.endpoint = Endpoint[countryCode] this.appKey = appKey this.appSecret = appSecret } async generateAccessToken(payload: { code: string // oauth code, get from app callback URL uuid?: string // unique identifier, anti-replay }) { const apiPath = '/auth/token/create' const body = { ...payload, ...makeSystemParameters(this.appKey, this.appSecret, apiPath, undefined, payload) } const response = await fetch(Endpoint.auth + apiPath, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) return await response.json() } async refreshAccessToken(payload: { refresh_token: string }) { /* CONFIRM-ME not qualified */ const apiPath = '/auth/token/refresh' const body = { ...payload, ...makeSystemParameters(this.appKey, this.appSecret, apiPath, undefined, payload) } const response = await fetch(Endpoint.auth + apiPath, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) return await response.json() } async get(path: string, params: Parameter, accessToken?: string) { const qs = Object.entries({ ...params, ...makeSystemParameters(this.appKey, this.appSecret, path, accessToken || this.accessToken, params) }) .map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`) .join('&') const response = await fetch(this.endpoint + path + '?' + qs, { headers: { 'Content-Type': 'application/json' } }) return await response.json() } async post(path: string, params: Parameter, accessToken?: string) { if ('payload' in params) { params.payload = xmlize(params.payload) } const { app_key, timestamp, access_token, sign_method, sign } = makeSystemParameters(this.appKey, this.appSecret, path, accessToken || this.accessToken, params) const body = new FormData() body.append('app_key', app_key) body.append('timestamp', timestamp) body.append('access_token', access_token) body.append('sign_method', sign_method) body.append('sign', sign) for(let key in params) { body.append(key, params[key]) } const response = await fetch(this.endpoint + path, { method: 'post', headers: body.getHeaders(), body }) return await response.json() } }