Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
feat: move options into http module
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 2, 2020
1 parent 860e2dd commit 7dfbcc6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
31 changes: 15 additions & 16 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface Request {
headers: Record<string, string>
}

interface ClientInfo extends MWSOptions {
interface ResourceInfo {
resource: Resource
version: string
action: string
Expand All @@ -38,25 +38,24 @@ const canonicalizeParameters = (parameters: Parameters): string => {
return sp.toString().replace(/\+/g, '%20')
}

export class HttpClient {
static withDefaults() {
return new HttpClient(({ url, method, headers }) =>
axios({ method, url, headers }).then((response) => response.data),
)
}
const defaultFetch = ({ url, method, headers }: Request) =>
axios({ method, url, headers }).then((response) => response.data)

// eslint-disable-next-line no-useless-constructor, no-empty-function
constructor(private fetch: <T>(meta: Request) => Promise<T>) {}
export class HttpClient {
constructor(
private options: MWSOptions,
private fetch: <T>(meta: Request) => Promise<T> = defaultFetch, // eslint-disable-next-line no-empty-function
) {}

request(method: HttpMethod, info: ClientInfo) {
const host = info.endpoint.replace('https://', '')
const url = `${info.endpoint}/${info.resource}/${info.version}/`
request(method: HttpMethod, info: ResourceInfo) {
const host = this.options.endpoint.replace('https://', '')
const url = `${this.options.endpoint}/${info.resource}/${info.version}/`

const parameters = {
AWSAccessKeyId: info.awsAccessKeyId,
AWSAccessKeyId: this.options.awsAccessKeyId,
Action: info.action,
MWSAuthToken: info.mwsAuthToken,
SellerId: info.sellerId,
MWSAuthToken: this.options.mwsAuthToken,
SellerId: this.options.sellerId,
SignatureMethod: 'HmacSHA256',
SignatureVersion: '2',
Timestamp: new Date().toISOString(),
Expand All @@ -67,7 +66,7 @@ export class HttpClient {
const parametersForSigning = canonicalizeParameters(parameters)
const queryStringToSign = `${method}\n${host}\n/${info.resource}/${info.version}\n${parametersForSigning}`

const signature = sign(queryStringToSign, info.secret)
const signature = sign(queryStringToSign, this.options.secret)
const parametersWithSignature = { ...parameters, Signature: signature }

return this.fetch({
Expand Down
11 changes: 4 additions & 7 deletions src/mws.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/* eslint-disable no-underscore-dangle */
import { HttpClient, MWSOptions } from './http'
import { HttpClient } from './http'
import { Sellers } from './sections/sellers'

export class MWS {
private httpClient: HttpClient

private _sellers!: Sellers

constructor(private options: MWSOptions) {
this.httpClient = options.httpClient ?? HttpClient.withDefaults()
}
// eslint-disable-next-line no-empty-function
constructor(private httpClient: HttpClient) {}

get sellers() {
if (!this._sellers) {
this._sellers = new Sellers(this.options, this.httpClient)
this._sellers = new Sellers(this.httpClient)
}

return this._sellers
Expand Down
5 changes: 2 additions & 3 deletions src/sections/sellers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { HttpClient, MWSOptions, Resource } from '../http'
import { HttpClient, Resource } from '../http'

export class Sellers {
// eslint-disable-next-line no-useless-constructor, no-empty-function
constructor(private options: MWSOptions, private httpClient: HttpClient) {}
constructor(private httpClient: HttpClient) {}

// TODO: type and parse response
listMarketplaceParticipations() {
return this.httpClient.request('GET', {
...this.options,
resource: Resource.Sellers,
version: '2011-07-01',
action: 'ListMarketplaceParticipations',
Expand Down

0 comments on commit 7dfbcc6

Please sign in to comment.