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

Commit

Permalink
Merge pull request #71 from ScaleLeap/feature/68-require-only-one
Browse files Browse the repository at this point in the history
feat: add RequireOnlyOne type and apply it to already made apis that use it
  • Loading branch information
justinemmanuelmercado committed Jun 5, 2020
2 parents d0c752d + c569bee commit a5dd54b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/sections/fulfillment-inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ParsingError } from '../error'
import { HttpClient, RequestMeta, Resource } from '../http'
import { ensureArray, mwsDate, NextToken, nextToken as nextTokenCodec } from '../parsing'
import { getServiceStatusByResource } from './shared'
import { RequireOnlyOne } from './types'

const FULFILLMENT_INVENTORY_API_VERSION = '2010-10-01'

Expand Down Expand Up @@ -112,7 +113,10 @@ export class FulfillmentInventory {
constructor(private httpClient: HttpClient) {}

async listInventorySupply(
parameters: ListInventorySupplyRequestParameters,
parameters: RequireOnlyOne<
ListInventorySupplyRequestParameters,
'MarketplaceId' | 'QueryStartDateTime'
>,
): Promise<[InventorySupplyList, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.FulfilmentInventory,
Expand Down
11 changes: 10 additions & 1 deletion src/sections/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
nextToken as nextTokenCodec,
} from '../parsing'
import { getServiceStatusByResource } from './shared'
import { RequireOnlyOne } from './types'

const ORDERS_API_VERSION = '2013-09-01'

Expand Down Expand Up @@ -322,7 +323,15 @@ const canonicalizeParameters = (parameters: ListOrderParameters) => {
export class Orders {
constructor(private httpClient: HttpClient) {}

async listOrders(parameters: ListOrderParameters): Promise<[ListOrders, RequestMeta]> {
/**
* If BuyerEmail is specified, then FulfillmentChannel,
* OrderStatus, PaymentMethod,
* LastUpdatedAfter, LastUpdatedBefore,
* and SellerOrderId cannot be specified.
*/
async listOrders(
parameters: RequireOnlyOne<ListOrderParameters, 'CreatedAfter' | 'LastUpdatedAfter'>,
): Promise<[ListOrders, RequestMeta]> {
const [response, meta] = await this.httpClient.request('POST', {
resource: Resource.Orders,
version: ORDERS_API_VERSION,
Expand Down
9 changes: 9 additions & 0 deletions src/sections/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* For use with parameters that require one of two values
* Source: https://stackoverflow.com/a/49725198/5808843
*/

export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>
}[Keys]
8 changes: 6 additions & 2 deletions test/unit/fulfillment-inventory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,22 @@ const parsingError = 'Expected an object, but received a string with value ""'

describe('fulfillment-inventory', () => {
describe('listInventorySupply', () => {
const parameters = {
MarketplaceId: '',
}

it('returns a parsed model when the response is valid', async () => {
expect.assertions(1)
expect(
await mockMwsInventorySupply.fulfillmentInventory.listInventorySupply({}),
await mockMwsInventorySupply.fulfillmentInventory.listInventorySupply(parameters),
).toMatchSnapshot()
})

it('throws a parsing error when the response is not valid', async () => {
expect.assertions(1)

await expect(() =>
mockMwsFail.fulfillmentInventory.listInventorySupply({}),
mockMwsFail.fulfillmentInventory.listInventorySupply(parameters),
).rejects.toStrictEqual(new ParsingError(parsingError))
})
})
Expand Down
11 changes: 6 additions & 5 deletions test/unit/orders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,19 @@ const parsingError = 'Expected an object, but received a string with value ""'

describe('orders', () => {
describe('listOrders', () => {
const parameters = { CreatedAfter: new Date(), MarketplaceId: [] }

it('returns a parsed model when the response is valid', async () => {
expect.assertions(1)

expect(await mockMwsListOrders.orders.listOrders({ MarketplaceId: [] })).toMatchSnapshot()
expect(await mockMwsListOrders.orders.listOrders(parameters)).toMatchSnapshot()
})

it('throws a parsing error when the response is not valid', async () => {
expect.assertions(1)

await expect(() =>
mockMwsFail.orders.listOrders({ MarketplaceId: [] }),
).rejects.toStrictEqual(new ParsingError(parsingError))
await expect(() => mockMwsFail.orders.listOrders(parameters)).rejects.toStrictEqual(
new ParsingError(parsingError),
)
})
})

Expand Down

0 comments on commit a5dd54b

Please sign in to comment.