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

Commit

Permalink
feat: init subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
justinemmanuelmercado committed Jun 15, 2020
1 parent f70210f commit 00dff3b
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ type Parameters = Record<string, ParameterTypes>
type CleanParameters = Record<string, string>

export enum Resource {
Sellers = 'Sellers',
FulfilmentInventory = 'FulfillmentInventory',
Orders = 'Orders',
Products = 'Products',
FulfilmentInventory = 'FulfillmentInventory',
Reports = 'Reports',
Sellers = 'Sellers',
Subscriptions = 'Subscriptions',
}

interface ResourceActions {
Expand Down Expand Up @@ -116,6 +117,17 @@ interface ResourceActions {
| 'GetReportScheduleListByNextToken'
| 'GetReportScheduleCount'
| 'UpdateReportAcknowledgements'
[Resource.Subscriptions]:
| 'RegisterDestination'
| 'DeregisterDestination'
| 'ListRegisteredDestinations'
| 'SendTestNotificationToDestination'
| 'CreateSubscription'
| 'GetSubscription'
| 'DeleteSubscription'
| 'ListSubscriptions'
| 'UpdateSubscription'
| 'GetServiceStatus'
}

interface Request {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
11 changes: 11 additions & 0 deletions src/mws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,8 @@ export class MWS {

private _reports!: Reports

private _subscriptions!: Subscriptions

constructor(private httpClient: HttpClient) {}

get sellers() {
Expand Down Expand Up @@ -57,4 +60,12 @@ export class MWS {

return this._reports
}

get subscriptions() {
if (!this._subscriptions) {
this._subscriptions = new Subscriptions(this.httpClient)
}

return this._subscriptions
}
}
16 changes: 16 additions & 0 deletions src/sections/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -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,
)
}
}
19 changes: 19 additions & 0 deletions test/integration/subscriptions.test.ts
Original file line number Diff line number Diff line change
@@ -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 */
17 changes: 17 additions & 0 deletions test/unit/__snapshots__/subscriptions.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
},
]
`;
55 changes: 55 additions & 0 deletions test/unit/subscriptions.test.ts
Original file line number Diff line number Diff line change
@@ -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),
)
})
})
})

0 comments on commit 00dff3b

Please sign in to comment.