Skip to content
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
1 change: 1 addition & 0 deletions packages/api-request-builder/src/create-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const requiredDefinitionProps = ['type', 'endpoint', 'features']

function getIdOrKey(params: Object): string {
if (params.id) return `/${params.id}`
if (params.orderNumber) return `/order-number=${params.orderNumber}`
if (params.key && !params.container) return `/key=${params.key}`
if (params.key && params.container)
return `/${params.container}/${params.key}`
Expand Down
2 changes: 2 additions & 0 deletions packages/api-request-builder/src/default-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export function setParams(params: ServiceBuilderParams) {
'withTotal',
'applyOrderEditTo',
'container',
'orderNumber',
]
Object.keys(params).forEach((key: string) => {
if (!knownKeys.includes(key)) throw new Error(`Unknown key "${key}"`)
Expand Down Expand Up @@ -173,6 +174,7 @@ export function setParams(params: ServiceBuilderParams) {
if (hasKey(params, 'priceCustomerGroup'))
this.priceCustomerGroup(params.priceCustomerGroup)
if (hasKey(params, 'priceChannel')) this.priceChannel(params.priceChannel)
if (hasKey(params, 'orderNumber')) this.byOrderNumber(params.orderNumber)

// query-search
if (params.text) this.text(params.text.value, params.text.language)
Expand Down
18 changes: 18 additions & 0 deletions packages/api-request-builder/src/query-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ export function byCustomerId(custId: string): Object {
return this
}

/**
* Set the given `orderNumber` to the `orderNumber` internal state of the service instance.
* For querying orders
*
* @param {number} orderNumber - An order number
* @throws If `orderNumber` is missing or invalid
* @return {Object} The instance of the service, can be chained.
*/
export function byOrderNumber(orderNumber: number): Object {
if (typeof orderNumber !== 'number')
throw new Error(
'Required argument for `byOrderNumber` is missing or invalid'
)

this.params.orderNumber = orderNumber
return this
}

/**
* Set the given `id` to the `cartId` internal state of the service instance.
* For querying shipping methods by cart id
Expand Down
7 changes: 7 additions & 0 deletions packages/api-request-builder/test/create-service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const expectedServiceProperties = [
'byCurrency',
'byCountry',
'byState',
'byOrderNumber',
]
const projectKey = 'my-project1'

Expand Down Expand Up @@ -423,6 +424,12 @@ describe('createService', () => {
)
})

test('should mix orderNumber and queryParams', () => {
expect(service.byOrderNumber(123).expand('baz').build()).toBe(
'/my-project1/test/order-number=123?expand=baz'
)
})

test('should mix cartId and queryParams', () => {
expect(service.byCartId('foo').expand('baz').build()).toBe(
'/my-project1/test?cartId=foo&expand=baz'
Expand Down
17 changes: 17 additions & 0 deletions packages/api-request-builder/test/query-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ describe('queryId', () => {
)
})

test('should set the orderNumber param', () => {
service.byOrderNumber(123)
expect(service.params.orderNumber).toBe(123)
})

test('should throw if orderNumber is missing', () => {
expect(() => service.byOrderNumber()).toThrow(
/Required argument for `byOrderNumber` is missing/
)
})

test('should throw if orderNumber is invalid', () => {
expect(() => service.byOrderNumber('hi')).toThrow(
/Required argument for `byOrderNumber` is missing/
)
})

test('should set the cartId param', () => {
service.byCartId('myCart')
expect(service.params.cartId).toBe('myCart')
Expand Down
3 changes: 3 additions & 0 deletions types/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ export type ServiceBuilderParams = {

// container
container?: ?string,

// params
orderNumber?: number,
}
export type ServiceBuilder = {
type: string,
Expand Down