From b5fd27f675a0daf5a9600b0f6eaf8014c6bf3d6c Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Mon, 30 Oct 2023 02:36:49 -0700 Subject: [PATCH] fix: type issues --- libs/client/src/function.ts | 7 ++++--- libs/proxy/src/index.ts | 18 +++++++++--------- libs/proxy/src/nextjs.ts | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/libs/client/src/function.ts b/libs/client/src/function.ts index 5713ea8..9babed3 100644 --- a/libs/client/src/function.ts +++ b/libs/client/src/function.ts @@ -41,10 +41,11 @@ export function buildUrl( const { host } = getConfig(); const method = (options.method ?? 'post').toLowerCase(); const path = (options.path ?? '').replace(/^\//, '').replace(/\/{2,}/, '/'); + const input = options.input; const params = - method === 'get' ? new URLSearchParams(options.input ?? {}) : undefined; - // TODO: change to params.size once it's officially supported - const queryParams = params && params['size'] ? `?${params.toString()}` : ''; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + method === 'get' && input ? new URLSearchParams(input as any) : undefined; + const queryParams = params ? `?${params.toString()}` : ''; const parts = id.split('/'); // if a fal.ai url is passed, just use it diff --git a/libs/proxy/src/index.ts b/libs/proxy/src/index.ts index 400e6bd..d5d17d0 100644 --- a/libs/proxy/src/index.ts +++ b/libs/proxy/src/index.ts @@ -7,6 +7,8 @@ const FAL_KEY_ID = process.env.FAL_KEY_ID || process.env.NEXT_PUBLIC_FAL_KEY_ID; const FAL_KEY_SECRET = process.env.FAL_KEY_SECRET || process.env.NEXT_PUBLIC_FAL_KEY_SECRET; +export type HeaderValue = string | string[] | undefined | null; + /** * The proxy behavior that is passed to the proxy handler. This is a subset of * request objects that are used by different frameworks, like Express and NextJS. @@ -16,8 +18,8 @@ export interface ProxyBehavior { method: string; // eslint-disable-next-line @typescript-eslint/no-explicit-any respondWith(status: number, data: string | any): ResponseType; - getHeaders(): Record; - getHeader(name: string): string | string[] | undefined; + getHeaders(): Record; + getHeader(name: string): HeaderValue; sendHeader(name: string, value: string): void; getBody(): Promise; } @@ -29,10 +31,8 @@ export interface ProxyBehavior { * @param request the header value. * @returns the header value as `string` or `undefined` if the header is not set. */ -function singleHeaderValue( - value: string | string[] | undefined -): string | undefined { - if (value === undefined) { +function singleHeaderValue(value: HeaderValue): string | undefined { + if (!value) { return undefined; } if (Array.isArray(value)) { @@ -79,7 +79,7 @@ export async function handleRequest( } // pass over headers prefixed with x-fal-* - const headers: Record = {}; + const headers: Record = {}; Object.keys(behavior.getHeaders()).forEach((key) => { if (key.toLowerCase().startsWith('x-fal-')) { headers[key.toLowerCase()] = behavior.getHeader(key); @@ -99,7 +99,7 @@ export async function handleRequest( 'content-type': 'application/json', 'user-agent': userAgent, 'x-fal-client-proxy': proxyUserAgent, - }, + } as HeadersInit, body: behavior.method?.toUpperCase() === 'GET' ? undefined @@ -113,7 +113,7 @@ export async function handleRequest( } }); - if (res.headers.get('content-type').includes('application/json')) { + if (res.headers.get('content-type')?.includes('application/json')) { const data = await res.json(); return behavior.respondWith(res.status, data); } diff --git a/libs/proxy/src/nextjs.ts b/libs/proxy/src/nextjs.ts index 022e2a7..064e947 100644 --- a/libs/proxy/src/nextjs.ts +++ b/libs/proxy/src/nextjs.ts @@ -18,7 +18,7 @@ export const PROXY_ROUTE = DEFAULT_PROXY_ROUTE; export const handler: NextApiHandler = async (request, response) => { return handleRequest({ id: 'nextjs-page-router', - method: request.method, + method: request.method || 'POST', respondWith: (status, data) => typeof data === 'string' ? response.status(status).json({ detail: data }) @@ -47,7 +47,7 @@ function fromHeaders(headers: Headers): Record { * @returns a promise that resolves when the request is handled. */ async function routeHandler(request: NextRequest) { - const responseHeaders = {}; + const responseHeaders: Record = {}; return await handleRequest({ id: 'nextjs-app-router', method: request.method,