diff --git a/src/http.ts b/src/http.ts index 8ce5a97a..48f8da83 100644 --- a/src/http.ts +++ b/src/http.ts @@ -62,11 +62,12 @@ type Parameters = Record type CleanParameters = Record export enum Resource { - Sellers = 'Sellers', + FulfilmentInventory = 'FulfillmentInventory', Orders = 'Orders', Products = 'Products', - FulfilmentInventory = 'FulfillmentInventory', Reports = 'Reports', + Sellers = 'Sellers', + Subscriptions = 'Subscriptions', } interface ResourceActions { @@ -116,6 +117,17 @@ interface ResourceActions { | 'GetReportScheduleListByNextToken' | 'GetReportScheduleCount' | 'UpdateReportAcknowledgements' + [Resource.Subscriptions]: + | 'RegisterDestination' + | 'DeregisterDestination' + | 'ListRegisteredDestinations' + | 'SendTestNotificationToDestination' + | 'CreateSubscription' + | 'GetSubscription' + | 'DeleteSubscription' + | 'ListSubscriptions' + | 'UpdateSubscription' + | 'GetServiceStatus' } interface Request { diff --git a/src/index.ts b/src/index.ts index f2823c70..a4451f2e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ export * from './http' export * from './mws' export * from './sections/sellers' +export * from './sections/subscriptions' export * from './sections/orders' export * from './sections/products/products' export * from './sections/fulfillment-inventory' diff --git a/src/mws.ts b/src/mws.ts index 78f68623..c20f6f6e 100644 --- a/src/mws.ts +++ b/src/mws.ts @@ -4,6 +4,7 @@ import { Orders } from './sections/orders' import { Products } from './sections/products/products' import { Reports } from './sections/reports' import { Sellers } from './sections/sellers' +import { Subscriptions } from './sections/subscriptions' export class MWS { private _sellers!: Sellers @@ -16,6 +17,8 @@ export class MWS { private _reports!: Reports + private _subscriptions!: Subscriptions + constructor(private httpClient: HttpClient) {} get sellers() { @@ -57,4 +60,12 @@ export class MWS { return this._reports } + + get subscriptions() { + if (!this._subscriptions) { + this._subscriptions = new Subscriptions(this.httpClient) + } + + return this._subscriptions + } } diff --git a/src/sections/subscriptions.ts b/src/sections/subscriptions.ts new file mode 100644 index 00000000..eae19567 --- /dev/null +++ b/src/sections/subscriptions.ts @@ -0,0 +1,16 @@ +import { HttpClient, Resource } from '../http' +import { getServiceStatusByResource } from './shared' + +const SUBSCRIPTIONS_API_VERSION = '2011-07-01' + +export class Subscriptions { + constructor(private httpClient: HttpClient) {} + + async getServiceStatus() { + return getServiceStatusByResource( + this.httpClient, + Resource.Subscriptions, + SUBSCRIPTIONS_API_VERSION, + ) + } +} diff --git a/test/integration/subscriptions.test.ts b/test/integration/subscriptions.test.ts new file mode 100644 index 00000000..e97edf5c --- /dev/null +++ b/test/integration/subscriptions.test.ts @@ -0,0 +1,19 @@ +import { Subscriptions } from '../../src' +import { Config } from './config' +import { itci } from './it' + +const httpClient = new Config().createHttpClient() + +/* eslint-disable jest/no-standalone-expect */ +describe('subscriptions', () => { + itci('should be able to query service status', async () => { + expect.assertions(1) + + const subscriptions = new Subscriptions(httpClient) + + const [response] = await subscriptions.getServiceStatus() + + expect(response.Status).toMatch(/GREEN|YELLOW|RED/) + }) +}) +/* eslint-enable jest/no-standalone-expect */ diff --git a/test/unit/__snapshots__/subscriptions.test.ts.snap b/test/unit/__snapshots__/subscriptions.test.ts.snap new file mode 100644 index 00000000..d06a202f --- /dev/null +++ b/test/unit/__snapshots__/subscriptions.test.ts.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`sellers getServiceStatus returns a parsed model when the status response is valid 1`] = ` +Array [ + Object { + "Status": "GREEN", + "Timestamp": "2020-05-06T08:22:23.582Z", + }, + Object { + "quotaMax": 1000, + "quotaRemaining": 999, + "quotaResetOn": 2020-04-06T10:22:23.582Z, + "requestId": "0", + "timestamp": 2020-05-06T09:22:23.582Z, + }, +] +`; diff --git a/test/unit/subscriptions.test.ts b/test/unit/subscriptions.test.ts new file mode 100644 index 00000000..6bb3e20b --- /dev/null +++ b/test/unit/subscriptions.test.ts @@ -0,0 +1,55 @@ +import { amazonMarketplaces, HttpClient, ParsingError } from '../../src' +import { MWS } from '../../src/mws' +import { getFixture } from '../utils' + +const httpConfig = { + awsAccessKeyId: '', + marketplace: amazonMarketplaces.CA, + mwsAuthToken: '', + secretKey: '', + sellerId: '', +} + +const headers = { + 'x-mws-request-id': '0', + 'x-mws-timestamp': '2020-05-06T09:22:23.582Z', + 'x-mws-quota-max': '1000', + 'x-mws-quota-remaining': '999', + 'x-mws-quota-resetson': '2020-04-06T10:22:23.582Z', +} + +const createMockHttpClient = (fixture: string) => + new MWS( + new HttpClient(httpConfig, () => + Promise.resolve({ + data: getFixture(fixture), + headers, + }), + ), + ) + +const mockMwsServiceStatus = createMockHttpClient('get_service_status') + +const mockMwsFail = new MWS( + new HttpClient(httpConfig, () => Promise.resolve({ data: '', headers: {} })), +) + +const parsingError = 'Expected an object, but received a string with value ""' + +describe('sellers', () => { + describe('getServiceStatus', () => { + it('returns a parsed model when the status response is valid', async () => { + expect.assertions(1) + + expect(await mockMwsServiceStatus.subscriptions.getServiceStatus()).toMatchSnapshot() + }) + + it('throws a parsing error when the status response is not valid', async () => { + expect.assertions(1) + + await expect(() => mockMwsFail.sellers.getServiceStatus()).rejects.toStrictEqual( + new ParsingError(parsingError), + ) + }) + }) +})