|
| 1 | +import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env"; |
| 2 | +import { StackAssertionError } from "@stackframe/stack-shared/dist/utils/errors"; |
| 3 | +import { deepPlainEquals, filterUndefined } from "@stackframe/stack-shared/dist/utils/objects"; |
| 4 | +import { deindent } from "@stackframe/stack-shared/dist/utils/strings"; |
| 5 | + |
| 6 | +export type EndpointOutput = { |
| 7 | + status: number, |
| 8 | + responseJson: any, |
| 9 | +}; |
| 10 | + |
| 11 | +export type OutputData = Record<string, EndpointOutput[]>; |
| 12 | + |
| 13 | +export type ExpectStatusCode = <T = any>( |
| 14 | + expectedStatusCode: number, |
| 15 | + endpoint: string, |
| 16 | + request: RequestInit, |
| 17 | +) => Promise<T>; |
| 18 | + |
| 19 | +export function createApiHelpers(options: { |
| 20 | + currentOutputData: OutputData, |
| 21 | + targetOutputData?: OutputData, |
| 22 | +}) { |
| 23 | + const { currentOutputData, targetOutputData } = options; |
| 24 | + |
| 25 | + function appendOutputData(endpoint: string, output: EndpointOutput) { |
| 26 | + if (!(endpoint in currentOutputData)) { |
| 27 | + currentOutputData[endpoint] = []; |
| 28 | + } |
| 29 | + const newLength = currentOutputData[endpoint].push(output); |
| 30 | + if (targetOutputData) { |
| 31 | + if (!(endpoint in targetOutputData)) { |
| 32 | + throw new StackAssertionError(deindent` |
| 33 | + Output data mismatch for endpoint ${endpoint}: |
| 34 | + Expected ${endpoint} to be in targetOutputData, but it is not. |
| 35 | + `, { endpoint }); |
| 36 | + } |
| 37 | + if (targetOutputData[endpoint].length < newLength) { |
| 38 | + throw new StackAssertionError(deindent` |
| 39 | + Output data mismatch for endpoint ${endpoint}: |
| 40 | + Expected ${targetOutputData[endpoint].length} outputs but got at least ${newLength}. |
| 41 | + `, { endpoint }); |
| 42 | + } |
| 43 | + if (!(deepPlainEquals(targetOutputData[endpoint][newLength - 1], output))) { |
| 44 | + throw new StackAssertionError(deindent` |
| 45 | + Output data mismatch for endpoint ${endpoint}: |
| 46 | + Expected output[${JSON.stringify(endpoint)}][${newLength - 1}] to be: |
| 47 | + ${JSON.stringify(targetOutputData[endpoint][newLength - 1], null, 2)} |
| 48 | + but got: |
| 49 | + ${JSON.stringify(output, null, 2)}. |
| 50 | + `, { endpoint }); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + const expectStatusCode: ExpectStatusCode = async (expectedStatusCode, endpoint, request) => { |
| 56 | + const apiUrl = new URL(getEnvVariable("NEXT_PUBLIC_STACK_API_URL")); |
| 57 | + const response = await fetch(new URL(endpoint, apiUrl), { |
| 58 | + ...request, |
| 59 | + headers: { |
| 60 | + "x-stack-disable-artificial-development-delay": "yes", |
| 61 | + "x-stack-development-disable-extended-logging": "yes", |
| 62 | + ...filterUndefined(request.headers ?? {}), |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const responseText = await response.text(); |
| 67 | + |
| 68 | + if (response.status !== expectedStatusCode) { |
| 69 | + throw new StackAssertionError(deindent` |
| 70 | + Expected status code ${expectedStatusCode} but got ${response.status} for ${endpoint}: |
| 71 | +
|
| 72 | + ${responseText} |
| 73 | + `, { request, response }); |
| 74 | + } |
| 75 | + |
| 76 | + const responseJson = JSON.parse(responseText); |
| 77 | + const currentOutput: EndpointOutput = { |
| 78 | + status: response.status, |
| 79 | + responseJson, |
| 80 | + }; |
| 81 | + |
| 82 | + appendOutputData(endpoint, currentOutput); |
| 83 | + |
| 84 | + return responseJson; |
| 85 | + }; |
| 86 | + |
| 87 | + return { |
| 88 | + appendOutputData, |
| 89 | + expectStatusCode, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
0 commit comments