This commit is contained in:
34
src/pages/api/_api.ts
Normal file
34
src/pages/api/_api.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { APIRoute } from 'astro';
|
||||
import { z } from 'astro:schema';
|
||||
import { checkApiBasicAuth } from '@util/auth.ts';
|
||||
|
||||
export function externalApi<InputSchema extends z.ZodType | undefined>(params: {
|
||||
input?: InputSchema;
|
||||
auth?: boolean;
|
||||
handler: ({
|
||||
input,
|
||||
params
|
||||
}: {
|
||||
input: InputSchema extends z.ZodType ? z.infer<InputSchema> : {};
|
||||
params: Record<string, string | undefined>;
|
||||
}) => Response | Promise<Response>;
|
||||
}): APIRoute {
|
||||
return async (context) => {
|
||||
if (params.auth && !checkApiBasicAuth(context.request.headers)) {
|
||||
return new Response(null, { status: 401 });
|
||||
}
|
||||
|
||||
let input;
|
||||
if (params.input) {
|
||||
try {
|
||||
input = await params.input.parseAsync(await context.request.json());
|
||||
} catch (_) {
|
||||
return new Response(null, { status: 400 });
|
||||
}
|
||||
} else {
|
||||
input = {};
|
||||
}
|
||||
|
||||
return params.handler({ input: input, params: context.params });
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user