- Pin pnpm to the exact version (10.10.0) when installing it in the production Dockerfile, matching packageManager and Dockerfile.e2e, instead of a floating major that can drift the lockfile behaviour. - Fix mockEndpoint's array header handling in the api test utility: append each value instead of joining with a comma, which is how multi-value headers (e.g. set-cookie) are actually represented. - Update apps/remark42's engines to node >=18 / pnpm >=10, matching the pnpm 10 requirement instead of the stale node 16 / pnpm 8 range.
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { http, HttpResponse } from 'msw'
|
|
import { setupServer } from 'msw/node'
|
|
|
|
export const server = setupServer()
|
|
|
|
interface CapturedRequest {
|
|
url: URL
|
|
method: string
|
|
headers: Headers
|
|
json: () => Promise<unknown>
|
|
text: () => Promise<string>
|
|
}
|
|
|
|
interface RequestRef {
|
|
req: CapturedRequest
|
|
}
|
|
|
|
export function mockEndpoint(
|
|
url: string,
|
|
params: {
|
|
method?: 'get' | 'put' | 'post' | 'delete'
|
|
body?: number | string | null | Record<string, unknown> | unknown[]
|
|
status?: number
|
|
headers?: Record<string, string | string[]>
|
|
} = {}
|
|
): RequestRef {
|
|
const { body, method = 'get', status = 200, headers } = params
|
|
const result = { req: {} } as RequestRef
|
|
|
|
server.use(
|
|
http[method](url, ({ request }) => {
|
|
const captured = request.clone()
|
|
result.req = {
|
|
url: new URL(request.url),
|
|
method: request.method,
|
|
headers: request.headers,
|
|
json: () => captured.clone().json(),
|
|
text: () => captured.clone().text(),
|
|
}
|
|
|
|
const responseHeaders = new Headers()
|
|
if (headers) {
|
|
for (const [key, value] of Object.entries(headers)) {
|
|
if (Array.isArray(value)) {
|
|
for (const v of value) responseHeaders.append(key, v)
|
|
} else {
|
|
responseHeaders.set(key, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
return body === undefined
|
|
? new HttpResponse(null, { status, headers: responseHeaders })
|
|
: HttpResponse.json(body, { status, headers: responseHeaders })
|
|
})
|
|
)
|
|
|
|
return result
|
|
}
|