-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add checkout session tags endpoints
- Loading branch information
Showing
12 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/client/src/checkout/__fixtures__/getCheckoutSessionTags.fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
14 changes: 14 additions & 0 deletions
14
packages/client/src/checkout/__fixtures__/putCheckoutSessionTags.fixtures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
11 changes: 11 additions & 0 deletions
11
packages/client/src/checkout/__tests__/__snapshots__/getCheckoutSessionTags.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
11 changes: 11 additions & 0 deletions
11
packages/client/src/checkout/__tests__/__snapshots__/putCheckoutSessionTags.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
37 changes: 37 additions & 0 deletions
37
packages/client/src/checkout/__tests__/getCheckoutSessionTags.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/client/src/checkout/__tests__/putCheckoutSessionTags.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
9 changes: 9 additions & 0 deletions
9
packages/client/src/checkout/types/getCheckoutSessionTags.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/client/src/checkout/types/putCheckoutSessionTags.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |