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

Revert "Revert "feature: subscriptions api"" #92

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ export interface MWSOptions {
}

type HttpMethod = 'GET' | 'POST'
type ParameterTypes = string | number | (number | string)[] | object[] | boolean | undefined
type ParameterTypes =
| string
| number
| (number | string)[]
| object[]
| boolean
| { [key: string]: ParameterTypes }
| undefined

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 @@ -117,6 +126,17 @@ interface ResourceActions {
| 'GetReportScheduleListByNextToken'
| 'GetReportScheduleCount'
| 'UpdateReportAcknowledgements'
[Resource.Subscriptions]:
| 'RegisterDestination'
| 'DeregisterDestination'
| 'ListRegisteredDestinations'
| 'SendTestNotificationToDestination'
| 'CreateSubscription'
| 'GetSubscription'
| 'DeleteSubscription'
| 'ListSubscriptions'
| 'UpdateSubscription'
| 'GetServiceStatus'
}

interface Request {
Expand Down Expand Up @@ -173,7 +193,15 @@ export const cleanParameters = (parameters: Parameters): CleanParameters =>
Object.entries(parameters)
.filter(([, parameter]) => parameter !== undefined)
.reduce((result, [key, parameter]) => {
if (Array.isArray(parameter)) {
if (typeof parameter === 'string' || !Number.isNaN(Number(parameter))) {
/**
* If parameter is type string or number, assign it to result
*/
Object.assign(result, { [key]: String(parameter) })
} else if (Array.isArray(parameter)) {
/**
* If parameter is type array reduce it to dotnotation
*/
parameter.forEach((parameterChild: string | number | object, index: number) => {
if (typeof parameterChild === 'string' || !Number.isNaN(Number(parameterChild))) {
Object.assign(result, { [`${key}.${index + 1}`]: String(parameterChild) })
Expand All @@ -182,7 +210,14 @@ export const cleanParameters = (parameters: Parameters): CleanParameters =>
}
})
} else {
Object.assign(result, { [key]: String(parameter) })
/**
* If parameter is type object parameterize it
*/
Object.entries(
cleanParameters(parameter as Parameters),
).forEach(([innerKey, innerValue]: [string, string]) =>
Object.assign(result, { [`${key}.${innerKey}`]: innerValue }),
)
}

return result
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
}
}
Loading