|
| 1 | +import { http } from 'msw'; |
| 2 | + |
| 3 | +import { cloudNATFactory } from 'src/factories'; |
| 4 | +import { mswDB } from 'src/mocks/indexedDB'; |
| 5 | +import { |
| 6 | + makeNotFoundResponse, |
| 7 | + makePaginatedResponse, |
| 8 | + makeResponse, |
| 9 | +} from 'src/mocks/utilities/response'; |
| 10 | + |
| 11 | +import type { CloudNAT } from '@linode/api-v4'; |
| 12 | +import type { StrictResponse } from 'msw'; |
| 13 | +import type { MockState } from 'src/mocks/types'; |
| 14 | +import type { |
| 15 | + APIErrorResponse, |
| 16 | + APIPaginatedResponse, |
| 17 | +} from 'src/mocks/utilities/response'; |
| 18 | + |
| 19 | +export const getCloudNATs = () => [ |
| 20 | + http.get( |
| 21 | + '*/v4/networking/cloudnats', |
| 22 | + async ({ |
| 23 | + request, |
| 24 | + }): Promise< |
| 25 | + StrictResponse<APIErrorResponse | APIPaginatedResponse<CloudNAT>> |
| 26 | + > => { |
| 27 | + const cloudNATs = await mswDB.getAll('cloudnats'); |
| 28 | + |
| 29 | + return makePaginatedResponse({ |
| 30 | + data: cloudNATs || [], |
| 31 | + request, |
| 32 | + }); |
| 33 | + } |
| 34 | + ), |
| 35 | + |
| 36 | + http.get( |
| 37 | + '*/v4/networking/cloudnats/:id', |
| 38 | + async ({ |
| 39 | + params, |
| 40 | + }): Promise<StrictResponse<APIErrorResponse | CloudNAT>> => { |
| 41 | + const id = Number(params.id); |
| 42 | + const cloudNAT = await mswDB.get('cloudnats', id); |
| 43 | + |
| 44 | + if (!cloudNAT) { |
| 45 | + return makeNotFoundResponse(); |
| 46 | + } |
| 47 | + |
| 48 | + return makeResponse(cloudNAT); |
| 49 | + } |
| 50 | + ), |
| 51 | +]; |
| 52 | + |
| 53 | +export const createCloudNAT = (mockState: MockState) => [ |
| 54 | + http.post( |
| 55 | + '*/v4/networking/cloudnats', |
| 56 | + async ({ |
| 57 | + request, |
| 58 | + }): Promise<StrictResponse<APIErrorResponse | CloudNAT>> => { |
| 59 | + const payload = await request.clone().json(); |
| 60 | + |
| 61 | + const cloudNAT = cloudNATFactory.build({ |
| 62 | + ...payload, |
| 63 | + addresses: ['203.0.113.100'], |
| 64 | + }); |
| 65 | + |
| 66 | + await mswDB.add('cloudnats', cloudNAT, mockState); |
| 67 | + |
| 68 | + return makeResponse(cloudNAT); |
| 69 | + } |
| 70 | + ), |
| 71 | +]; |
| 72 | + |
| 73 | +export const updateCloudNAT = (mockState: MockState) => [ |
| 74 | + http.put( |
| 75 | + '*/v4/networking/cloudnats/:id', |
| 76 | + async ({ |
| 77 | + params, |
| 78 | + request, |
| 79 | + }): Promise<StrictResponse<APIErrorResponse | CloudNAT>> => { |
| 80 | + const id = Number(params.id); |
| 81 | + const cloudNAT = await mswDB.get('cloudnats', id); |
| 82 | + |
| 83 | + if (!cloudNAT) { |
| 84 | + return makeNotFoundResponse(); |
| 85 | + } |
| 86 | + |
| 87 | + const payload = await request.clone().json(); |
| 88 | + const updatedCloudNAT = { ...cloudNAT, ...payload }; |
| 89 | + |
| 90 | + await mswDB.update('cloudnats', id, updatedCloudNAT, mockState); |
| 91 | + |
| 92 | + return makeResponse(updatedCloudNAT); |
| 93 | + } |
| 94 | + ), |
| 95 | +]; |
| 96 | + |
| 97 | +export const deleteCloudNAT = (mockState: MockState) => [ |
| 98 | + http.delete( |
| 99 | + '*/v4/networking/cloudnats/:id', |
| 100 | + async ({ params }): Promise<StrictResponse<APIErrorResponse | {}>> => { |
| 101 | + const id = Number(params.id); |
| 102 | + const cloudNAT = await mswDB.get('cloudnats', id); |
| 103 | + |
| 104 | + if (!cloudNAT) { |
| 105 | + return makeNotFoundResponse(); |
| 106 | + } |
| 107 | + |
| 108 | + await mswDB.delete('cloudnats', id, mockState); |
| 109 | + |
| 110 | + return makeResponse({}); |
| 111 | + } |
| 112 | + ), |
| 113 | +]; |
0 commit comments