Skip to content

Commit

Permalink
feat: use custom request instead of popsicle
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed Oct 16, 2019
1 parent b3563b1 commit c701ccd
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/o-auth-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import ClientOAuth2 from 'client-oauth2'
import { Options } from 'client-oauth2'
import fetch, { Headers } from 'cross-fetch'
import { defaultsDeep } from 'lodash'

const request: ClientOAuth2.Request = async (method, url, body, headerRecord): ReturnType<ClientOAuth2.Request> => {
const headers = new Headers()
headers.append('Accept-Encoding', 'application/json') // disable compression
Object.keys(headerRecord).map(key => headers.append(key, headerRecord[key] as string))

const req: RequestInit = {
method,
headers,
body,
}

const res = await fetch(url, req)

return {
status: res.status,
body: await res.text(),
}
}

export class OAuthClient {
// https://advertising.amazon.com/API/docs/v2/guides/authorization
private readonly amazonOptions: Options = {
Expand All @@ -10,9 +30,9 @@ export class OAuthClient {
scopes: ['cpc_advertising:campaign_management'],
}

private readonly client = new ClientOAuth2(defaultsDeep({}, this.opts, this.amazonOptions))
private readonly client = new ClientOAuth2(defaultsDeep({}, this.opts, this.amazonOptions), request)

public constructor(private readonly opts: Options) {}
public constructor(private readonly opts: Options) { }

public get getUri() {
return this.client.code.getUri.bind(this)
Expand Down

0 comments on commit c701ccd

Please sign in to comment.