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

Commit

Permalink
feat: parse and return meta from headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gigobyte committed May 4, 2020
1 parent 19d1af0 commit 774a1e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
29 changes: 25 additions & 4 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,46 @@ interface ResourceInfo<TResource extends Resource> {
parameters: Parameters
}

export interface RequestMeta {
requestId: string
timestamp: string
quotaMax: number
quotaRemaining: number
quotaResetOn: string
}

const canonicalizeParameters = (parameters: Parameters): string => {
const sp = new URLSearchParams(parameters)
sp.sort()
return sp.toString().replace(/\+/g, '%20')
}

const defaultFetch = <T>({ url, method, headers, data }: Request): Promise<T> =>
axios({ method, url, headers, data }).then((response) => parser.parse(response.data))
const defaultFetch = <T>({ url, method, headers, data }: Request): Promise<[T, RequestMeta]> =>
axios({ method, url, headers, data }).then((response) => {
const responseData = parser.parse(response.data)

return [
responseData,
{
requestId: response.headers['x-mws-request-id'] ?? responseData.ResponseMetadata.RequestId,
timestamp: response.headers['x-mws-timestamp'],
quotaMax: Number(response.headers['x-mws-quota-max']),
quotaRemaining: Number(response.headers['x-mws-quota-remaining']),
quotaResetOn: response.headers['x-mws-quota-resetson'],
},
]
})

export class HttpClient {
constructor(
private options: MWSOptions,
private fetch: <T>(meta: Request) => Promise<T> = defaultFetch,
private fetch: <T>(meta: Request) => Promise<[T, RequestMeta]> = defaultFetch,
) {}

request<TResource extends Resource, TRes>(
method: HttpMethod,
info: ResourceInfo<TResource>,
): Promise<TRes> {
): Promise<[TRes, RequestMeta]> {
const marketplaceUri = this.options.marketplace.webServiceUri

const host = marketplaceUri.replace('https://', '')
Expand Down
11 changes: 7 additions & 4 deletions src/sections/sellers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from '@sinclair/typebox'
import Ajv from 'ajv'

import { HttpClient, Resource } from '../http'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureArray, parseBoolean } from '../parsing'

interface MarketplaceParticipationsResponse {
Expand Down Expand Up @@ -71,8 +71,11 @@ interface MarketplaceParticipations {
export class Sellers {
constructor(private httpClient: HttpClient) {}

async listMarketplaceParticipations(): Promise<MarketplaceParticipations> {
const response: MarketplaceParticipationsResponse = await this.httpClient.request('POST', {
async listMarketplaceParticipations(): Promise<[MarketplaceParticipations, RequestMeta]> {
const [response, meta]: [
MarketplaceParticipationsResponse,
RequestMeta,
] = await this.httpClient.request('POST', {
resource: Resource.Sellers,
version: '2011-07-01',
action: 'ListMarketplaceParticipations',
Expand All @@ -98,7 +101,7 @@ export class Sellers {
}

if (new Ajv().validate(MarketplaceParticipations, result)) {
return result
return [result, meta]
}

throw new Error('TODO for now')
Expand Down

0 comments on commit 774a1e8

Please sign in to comment.