Skip to content

Commit

Permalink
feat(client): add checkout session tags endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ivomota committed Sep 15, 2023
1 parent 62d42ac commit 922cc4c
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { rest, type RestHandler } from 'msw';
import type { CheckoutSessionTags } from '../types/index.js';

const path = '/api/checkout/v1/checkoutSessions/:id/tags';

const fixtures = {
success: (response: CheckoutSessionTags): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(200), ctx.json(response)),
),
failure: (): RestHandler =>
rest.get(path, (_req, res, ctx) =>
res(ctx.status(404), ctx.json({ message: 'stub error' })),
),
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { rest, type RestHandler } from 'msw';

const path = '/api/checkout/v1/checkoutSessions/:id/tags';

const fixtures = {
success: (): RestHandler =>
rest.put(path, (_req, res, ctx) => res(ctx.status(204))),
failure: (): RestHandler =>
rest.put(path, (_req, res, ctx) =>
res(ctx.status(404), ctx.json({ message: 'stub error' })),
),
};

export default fixtures;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client getCheckoutSessionTags should receive a client request error 1`] = `
Object {
"code": "-1",
"message": "stub error",
"name": "AxiosError",
"status": 404,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`checkout client putCheckoutSessionTags should receive a client request error 1`] = `
Object {
"code": "-1",
"message": "stub error",
"name": "AxiosError",
"status": 404,
"transportLayerErrorCode": "ERR_BAD_REQUEST",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getCheckoutSessionTags } from '../index.js';
import { mockCheckoutSessionId } from 'tests/__fixtures__/checkout/index.mjs';
import client from '../../helpers/client/index.js';
import fixtures from '../__fixtures__/getCheckoutSessionTags.fixtures.js';
import mswServer from '../../../tests/mswServer.js';
import type { CheckoutSessionTags } from '../types/index.js';

describe('checkout client', () => {
const expectedConfig = undefined;

beforeEach(() => jest.clearAllMocks());

describe('getCheckoutSessionTags', () => {
const spy = jest.spyOn(client, 'get');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}/tags`;

it('should handle a client request successfully', async () => {
const response: CheckoutSessionTags = ['tag1'];

mswServer.use(fixtures.success(response));

await expect(
getCheckoutSessionTags(mockCheckoutSessionId),
).resolves.toStrictEqual(response);
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});

it('should receive a client request error', async () => {
mswServer.use(fixtures.failure());

await expect(
getCheckoutSessionTags(mockCheckoutSessionId),
).rejects.toMatchSnapshot();
expect(spy).toHaveBeenCalledWith(urlToBeCalled, expectedConfig);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import client from '../../helpers/client/index.js';

import putCheckoutSessionTags from '../putCheckoutSessionTags.js';

import mswServer from '../../../tests/mswServer.js';

import { mockCheckoutSessionId } from 'tests/__fixtures__/checkout/checkoutSessions.fixtures.mjs';
import fixtures from '../__fixtures__/putCheckoutSessionTags.fixtures.js';

import type { CheckoutSessionTags } from '../types/index.js';

describe('checkout client', () => {
const expectedConfig = undefined;

beforeEach(() => jest.clearAllMocks());

describe('putCheckoutSessionTags', () => {
const data: CheckoutSessionTags = ['someTag'];

const spy = jest.spyOn(client, 'put');
const urlToBeCalled = `/checkout/v1/checkoutSessions/${mockCheckoutSessionId}/tags`;

it('should handle a client request successfully', async () => {
mswServer.use(fixtures.success());

await expect(
putCheckoutSessionTags(mockCheckoutSessionId, data),
).resolves.toBe(204);

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});

it('should receive a client request error', async () => {
mswServer.use(fixtures.failure());

await expect(
putCheckoutSessionTags(mockCheckoutSessionId, data),
).rejects.toMatchSnapshot();

expect(spy).toHaveBeenCalledWith(urlToBeCalled, data, expectedConfig);
});
});
});
28 changes: 28 additions & 0 deletions packages/client/src/checkout/getCheckoutSessionTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { GetCheckoutSessionTags } from './types/getCheckoutSessionTags.types.js';

/**
* Method responsible for getting the checkout session tags.
*
* @param checkoutSessionId - Id of the checkout session to get.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const getCheckoutSessionTags: GetCheckoutSessionTags = (
checkoutSessionId,
config,
) =>
client
.get(
join('/checkout/v1/checkoutSessions', checkoutSessionId, 'tags'),
config,
)
.then(response => response.data)
.catch(error => {
throw adaptError(error);
});

export default getCheckoutSessionTags;
2 changes: 2 additions & 0 deletions packages/client/src/checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { default as getCheckoutOrderOperations } from './getCheckoutOrderOperati
export { default as getCheckoutOrderPaymentMethods } from './getCheckoutOrderPaymentMethods.js';
export { default as getCheckoutSession } from './getCheckoutSession.js';
export { default as getCheckoutSessionCharge } from './getCheckoutSessionCharge.js';
export { default as getCheckoutSessionTags } from './getCheckoutSessionTags.js';
export { default as getCollectPoints } from './getCollectPoints.js';
export { default as patchCheckoutOrder } from './patchCheckoutOrder.js';
export { default as patchCheckoutOrderDeliveryBundleUpgrades } from './patchCheckoutOrderDeliveryBundleUpgrades.js';
Expand All @@ -32,6 +33,7 @@ export { default as putCheckoutOrderItemTags } from './putCheckoutOrderItemTags.
export { default as putCheckoutOrderPromocodes } from './putCheckoutOrderPromocodes.js';
export { default as putCheckoutOrderTags } from './putCheckoutOrderTags.js';
export { default as putCheckoutSessionPromocodes } from './putCheckoutSessionPromocodes.js';
export { default as putCheckoutSessionTags } from './putCheckoutSessionTags.js';

/**
* Checkout types.
Expand Down
31 changes: 31 additions & 0 deletions packages/client/src/checkout/putCheckoutSessionTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { adaptError } from '../helpers/client/formatError.js';
import client from '../helpers/client/index.js';
import join from 'proper-url-join';
import type { PutCheckoutSessionTags } from './types/index.js';

/**
* Method responsible for setting tags for the checkout session.
*
* @param checkoutSessionId - Universal identifier of the Checkout Session.
* @param data - Tags data.
* @param config - Custom configurations to send to the client instance (axios).
*
* @returns Promise that will resolve when the call to the endpoint finishes.
*/
const putCheckoutSessionTags: PutCheckoutSessionTags = (
checkoutSessionId,
data,
config,
) =>
client
.put(
join('/checkout/v1/checkoutSessions', checkoutSessionId, 'tags'),
data,
config,
)
.then(response => response.status)
.catch(error => {
throw adaptError(error);
});

export default putCheckoutSessionTags;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CheckoutSession } from './checkoutSession.types.js';
import type { Config } from '../../types/index.js';

export type CheckoutSessionTags = string[];

export type GetCheckoutSessionTags = (
checkoutSessionId: CheckoutSession['id'],
config?: Config,
) => Promise<CheckoutSessionTags>;
2 changes: 2 additions & 0 deletions packages/client/src/checkout/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './getCheckoutOrderPaymentMethods.types.js';
export * from './getCheckoutOrderResponse.types.js';
export * from './getCheckoutSession.types.js';
export * from './getCheckoutSessionCharge.types.js';
export * from './getCheckoutSessionTags.types.js';
export * from './getCollectPoints.types.js';
export * from './itemDeliveryProvisioning.types.js';
export * from './patchCheckoutOrder.types.js';
Expand All @@ -44,5 +45,6 @@ export * from './putCheckoutOrderItemTags.types.js';
export * from './putCheckoutOrderPromocodes.types.js';
export * from './putCheckoutOrderTags.types.js';
export * from './putCheckoutSessionPromocodes.types.js';
export * from './putCheckoutSessionTags.types.js';
export * from './shippingCostType.types.js';
export * from './shippingOption.types.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CheckoutSession } from './checkoutSession.types.js';
import type { CheckoutSessionTags } from './getCheckoutSessionTags.types.js';
import type { Config } from '../../types/index.js';

export type PutCheckoutSessionTags = (
checkoutSessionId: CheckoutSession['id'],
data: CheckoutSessionTags,
config?: Config,
) => Promise<number>;

0 comments on commit 922cc4c

Please sign in to comment.