UNPKG

@swell/cli

Version:

Swell's command line interface/utility

113 lines (101 loc) 2.85 kB
export function mockRequestTemplate(options) { return `\ import { env } from "cloudflare:test"; import { createSwellClient } from "./swell-client"; export interface MockRequestOptions { data?: SwellData; query?: { [key: string]: string }; session?: { [key: string]: any }; headers?: Record<string, string>; method?: string; url?: string; swell?: Partial<SwellAPI>; store?: Partial<SwellStore>; appId?: string; useRealSwell?: boolean; } function createDefaultSwellMock(): Partial<SwellAPI> { const notMocked = (method: string) => () => { throw new Error( \`swell.\${method}() not mocked. Pass { swell: { \${method}: vi.fn() } } to createMockRequest().\` ); }; return { get: notMocked("get"), post: notMocked("post"), put: notMocked("put"), delete: notMocked("delete"), settings: notMocked("settings"), }; } export function createMockRequest(options: MockRequestOptions = {}): SwellRequest { const { data = {}, query = {}, session = {}, headers = {}, method = "POST", url = "https://example.com/test", swell, store, appId, useRealSwell = false, } = options; const requestHeaders = new Headers(headers); const originalRequest = new Request(url, { method, headers: requestHeaders, body: method === "GET" ? undefined : JSON.stringify(data), }); const storeId = store?.id || env.SWELL_STORE_ID || "test-store"; const resolvedStore = { id: storeId, url: store?.url || "", admin_url: store?.admin_url || "", }; const context = { waitUntil: async (promise: Promise<unknown>) => { await promise; }, }; const swellClient = swell || (useRealSwell ? createSwellClient() : createDefaultSwellMock()); const resolvedAppId = appId || env.SWELL_APP_ID || "${options.appId}"; const req: Partial<SwellRequest> & { appId: string; storeId: string } = { originalRequest, context, url, method, headers: requestHeaders, referrer: originalRequest.referrer, credentials: "include", appId: resolvedAppId, storeId, accessToken: null, publicKey: null, store: resolvedStore, session, apiHost: env.SWELL_API_BASE_URL || "", logParams: undefined, swell: swellClient as SwellAPI, body: data, data, query, initialize: async () => {}, parseJson: (input: string) => JSON.parse(input), appValues: (idOrValues: string | SwellData, values?: SwellData) => { const targetAppId = typeof idOrValues === "string" ? idOrValues : resolvedAppId; const appValues = typeof idOrValues === "string" ? values : idOrValues; if (!targetAppId || !appValues) return undefined; return { $app: { [targetAppId]: appValues, }, }; }, }; return req as SwellRequest; } `; }