Skip to content

Commit 155353d

Browse files
authored
feat!: migrate to msw 2.0 (#294)
1 parent 88d1595 commit 155353d

17 files changed

+950
-2801
lines changed

package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"scripts": {
1010
"start": "tsc -w",
1111
"format": "prettier src/**/*.ts --write",
12-
"test": "jest -c test/jest.config.ts",
13-
"test:ts": "tsc -p test/tsconfig.test-d.json",
12+
"test": "vitest -c test/vitest.config.ts",
13+
"test:ts": "tsc -p test/typings/tsconfig.json",
1414
"clean": "rimraf ./lib",
1515
"build": "yarn clean && tsc",
1616
"release": "release publish",
@@ -27,20 +27,19 @@
2727
"@faker-js/faker": "^6.3.1",
2828
"@ossjs/release": "^0.8.0",
2929
"@types/debug": "^4.1.7",
30-
"@types/jest": "^26.0.22",
3130
"@types/node-fetch": "^2.5.10",
3231
"commitizen": "^4.2.4",
3332
"cz-conventional-changelog": "3.3.0",
34-
"jest": "^26.6.0",
35-
"msw": "^1.0.0",
33+
"jsdom": "^22.1.0",
34+
"msw": "^2.0.8",
3635
"node-fetch": "^2.6.1",
3736
"page-with": "^0.6.0",
3837
"prettier": "^2.2.1",
3938
"rimraf": "^3.0.2",
4039
"simple-git-hooks": "^2.7.0",
41-
"ts-jest": "^26.5.5",
4240
"ts-node": "^9.1.1",
43-
"typescript": "4.3.5"
41+
"typescript": "4.3.5",
42+
"vitest": "^0.34.6"
4443
},
4544
"dependencies": {
4645
"@types/lodash": "^4.14.172",
@@ -58,11 +57,11 @@
5857
"uuid": "^8.3.1"
5958
},
6059
"optionalDependencies": {
61-
"msw": "^1.0.0"
60+
"msw": "^2.0.8"
6261
},
6362
"config": {
6463
"commitizen": {
6564
"path": "./node_modules/cz-conventional-changelog"
6665
}
6766
}
68-
}
67+
}

src/glossary.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLSchema } from 'graphql'
2-
import { GraphQLHandler, RestHandler } from 'msw'
2+
import { GraphQLHandler, HttpHandler } from 'msw'
33
import { Database } from './db/Database'
44
import { NullableObject, NullableProperty } from './nullable'
55
import { PrimaryKey } from './primaryKey'
@@ -167,7 +167,7 @@ export interface ModelAPI<
167167
/**
168168
* Generate request handlers of the given type based on the model definition.
169169
*/
170-
toHandlers(type: 'rest', baseUrl?: string): RestHandler[]
170+
toHandlers(type: 'rest', baseUrl?: string): HttpHandler[]
171171
/**
172172
* Generate request handlers of the given type based on the model definition.
173173
*/

src/model/generateGraphQLHandlers.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import {
1414
GraphQLInputType,
1515
GraphQLScalarType,
1616
GraphQLFieldConfigArgumentMap,
17-
GraphQLError,
1817
} from 'graphql'
19-
import { GraphQLHandler, graphql } from 'msw'
18+
import { GraphQLHandler, HttpResponse, graphql } from 'msw'
2019
import { ModelAPI, ModelDefinition, ModelDictionary } from '../glossary'
2120
import { PrimaryKey } from '../primaryKey'
2221
import { capitalize } from '../utils/capitalize'
@@ -289,18 +288,14 @@ export function generateGraphQLHandlers<
289288
const objectSchema = generateGraphQLSchema(modelName, definition, model)
290289

291290
return [
292-
target.operation(async (req, res, ctx) => {
293-
if (!req.body) {
294-
return
295-
}
296-
291+
target.operation(async ({ query, variables }) => {
297292
const result = await executeGraphQL({
298293
schema: objectSchema,
299-
source: req.body?.query,
300-
variableValues: req.variables,
294+
source: query,
295+
variableValues: variables,
301296
})
302297

303-
return res(ctx.data(result.data!), ctx.errors(result.errors))
298+
return HttpResponse.json(result)
304299
}),
305300
]
306301
}

src/model/generateRestHandlers.ts

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import pluralize from 'pluralize'
22
import {
3-
RestContext,
4-
RestRequest,
53
ResponseResolver,
6-
rest,
4+
http,
75
DefaultBodyType,
86
PathParams,
7+
HttpResponse,
98
} from 'msw'
109
import {
1110
Entity,
@@ -65,25 +64,18 @@ export function getResponseStatusByErrorType(
6564
export function withErrors<
6665
RequestBodyType extends DefaultBodyType = any,
6766
RequestParamsType extends PathParams = any,
68-
>(
69-
handler: ResponseResolver<
70-
RestRequest<RequestBodyType, RequestParamsType>,
71-
RestContext
72-
>,
73-
): ResponseResolver<
74-
RestRequest<RequestBodyType, RequestParamsType>,
75-
RestContext
76-
> {
77-
return (req, res, ctx) => {
67+
>(resolver: ResponseResolver<any, RequestBodyType, DefaultBodyType>) {
68+
return async (...args: Parameters<ResponseResolver>): Promise<any> => {
7869
try {
79-
return handler(req, res, ctx)
70+
const response = await resolver(...args)
71+
return response
8072
} catch (error) {
8173
if (error instanceof Error) {
82-
return res(
83-
ctx.status(getResponseStatusByErrorType(error as HTTPError)),
84-
ctx.json({
85-
message: error.message,
86-
}),
74+
return HttpResponse.json(
75+
{ message: error.message },
76+
{
77+
status: getResponseStatusByErrorType(error as HTTPError),
78+
},
8779
)
8880
}
8981
}
@@ -153,13 +145,14 @@ export function generateRestHandlers<
153145
}
154146

155147
return [
156-
rest.get(
148+
http.get(
157149
buildUrl(modelPath),
158-
withErrors<Entity<Dictionary, ModelName>>((req, res, ctx) => {
150+
withErrors<Entity<Dictionary, ModelName>>(({ request }) => {
151+
const url = new URL(request.url)
159152
const { skip, take, cursor, filters } = parseQueryParams(
160153
modelName,
161154
modelDefinition,
162-
req.url.searchParams,
155+
url.searchParams,
163156
)
164157

165158
let options = { where: filters }
@@ -172,14 +165,14 @@ export function generateRestHandlers<
172165

173166
const records = model.findMany(options)
174167

175-
return res(ctx.json(records))
168+
return HttpResponse.json(records)
176169
}),
177170
),
178-
rest.get(
171+
http.get(
179172
buildUrl(`${modelPath}/:${primaryKey}`),
180173
withErrors<Entity<Dictionary, ModelName>, RequestParams<PrimaryKeyType>>(
181-
(req, res, ctx) => {
182-
const id = extractPrimaryKey(req.params)
174+
({ params }) => {
175+
const id = extractPrimaryKey(params)
183176
const where: WeakQuerySelectorWhere<PrimaryKeyType> = {
184177
[primaryKey]: {
185178
equals: id as string,
@@ -190,22 +183,24 @@ export function generateRestHandlers<
190183
where: where as any,
191184
})
192185

193-
return res(ctx.json(entity))
186+
return HttpResponse.json(entity)
194187
},
195188
),
196189
),
197-
rest.post(
190+
http.post(
198191
buildUrl(modelPath),
199-
withErrors<Entity<Dictionary, ModelName>>((req, res, ctx) => {
200-
const createdEntity = model.create(req.body)
201-
return res(ctx.status(201), ctx.json(createdEntity))
192+
withErrors<Entity<Dictionary, ModelName>>(async ({ request }) => {
193+
const definition = await request.json()
194+
const createdEntity = model.create(definition)
195+
196+
return HttpResponse.json(createdEntity, { status: 201 })
202197
}),
203198
),
204-
rest.put(
199+
http.put(
205200
buildUrl(`${modelPath}/:${primaryKey}`),
206201
withErrors<Entity<Dictionary, ModelName>, RequestParams<PrimaryKeyType>>(
207-
(req, res, ctx) => {
208-
const id = extractPrimaryKey(req.params)
202+
async ({ request, params }) => {
203+
const id = extractPrimaryKey(params)
209204
const where: WeakQuerySelectorWhere<PrimaryKeyType> = {
210205
[primaryKey]: {
211206
equals: id as string,
@@ -214,18 +209,18 @@ export function generateRestHandlers<
214209
const updatedEntity = model.update({
215210
strict: true,
216211
where: where as any,
217-
data: req.body,
212+
data: await request.json(),
218213
})!
219214

220-
return res(ctx.json(updatedEntity))
215+
return HttpResponse.json(updatedEntity)
221216
},
222217
),
223218
),
224-
rest.delete(
219+
http.delete(
225220
buildUrl(`${modelPath}/:${primaryKey}`),
226221
withErrors<Entity<Dictionary, ModelName>, RequestParams<PrimaryKeyType>>(
227-
(req, res, ctx) => {
228-
const id = extractPrimaryKey(req.params)
222+
({ params }) => {
223+
const id = extractPrimaryKey(params)
229224
const where: WeakQuerySelectorWhere<PrimaryKeyType> = {
230225
[primaryKey]: {
231226
equals: id as string,
@@ -236,7 +231,7 @@ export function generateRestHandlers<
236231
where: where as any,
237232
})!
238233

239-
return res(ctx.json(deletedEntity))
234+
return HttpResponse.json(deletedEntity)
240235
},
241236
),
242237
),

0 commit comments

Comments
 (0)