Skip to content

Commit

Permalink
fix: type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Oct 30, 2023
1 parent 4715e5c commit b5fd27f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
7 changes: 4 additions & 3 deletions libs/client/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ export function buildUrl<Input>(
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
Expand Down
18 changes: 9 additions & 9 deletions libs/proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -16,8 +18,8 @@ export interface ProxyBehavior<ResponseType> {
method: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
respondWith(status: number, data: string | any): ResponseType;
getHeaders(): Record<string, string | string[] | undefined>;
getHeader(name: string): string | string[] | undefined;
getHeaders(): Record<string, HeaderValue>;
getHeader(name: string): HeaderValue;
sendHeader(name: string, value: string): void;
getBody(): Promise<string | undefined>;
}
Expand All @@ -29,10 +31,8 @@ export interface ProxyBehavior<ResponseType> {
* @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)) {
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function handleRequest<ResponseType>(
}

// pass over headers prefixed with x-fal-*
const headers: Record<string, string | string[] | undefined> = {};
const headers: Record<string, HeaderValue> = {};
Object.keys(behavior.getHeaders()).forEach((key) => {
if (key.toLowerCase().startsWith('x-fal-')) {
headers[key.toLowerCase()] = behavior.getHeader(key);
Expand All @@ -99,7 +99,7 @@ export async function handleRequest<ResponseType>(
'content-type': 'application/json',
'user-agent': userAgent,
'x-fal-client-proxy': proxyUserAgent,
},
} as HeadersInit,
body:
behavior.method?.toUpperCase() === 'GET'
? undefined
Expand All @@ -113,7 +113,7 @@ export async function handleRequest<ResponseType>(
}
});

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);
}
Expand Down
4 changes: 2 additions & 2 deletions libs/proxy/src/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -47,7 +47,7 @@ function fromHeaders(headers: Headers): Record<string, string | string[]> {
* @returns a promise that resolves when the request is handled.
*/
async function routeHandler(request: NextRequest) {
const responseHeaders = {};
const responseHeaders: Record<string, any> = {};

Check warning on line 50 in libs/proxy/src/nextjs.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
return await handleRequest({
id: 'nextjs-app-router',
method: request.method,
Expand Down

0 comments on commit b5fd27f

Please sign in to comment.