0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

feat(cloud): add status apis (#3159)

This commit is contained in:
Gao Sun 2023-02-21 10:46:35 +08:00 committed by GitHub
parent 4d54b1ca86
commit f6eb8cf681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View file

@ -13,12 +13,14 @@ const { default: withSpa } = await import('./middleware/with-spa.js');
const { EnvSet } = await import('./env-set/index.js');
const { default: router } = await import('./routes/index.js');
const { default: anonymousRouter } = await import('./routes-anonymous/index.js');
const ignorePathnames = ['/api'];
const { listen } = createServer({
port: 3003,
composer: compose(withRequest())
.and(anonymousRouter.routes())
.and(
withPathname(
'/api',

View file

@ -8,7 +8,10 @@ import type {
import { matchPathname } from '#src/utils/url.js';
/**
* Build a middleware function that conditionally runs the given middleware function based on pathname prefix.
* Build a middleware function that conditionally runs the given middleware function when:
*
* - The current pathname matches the given pathname prefix; and
* - `context.status` is unset (i.e. `undefined`).
*
* @param pathname The pathname prefix to match.
* @param run The middleware function to run with the prefix matches.
@ -22,7 +25,7 @@ export default function withPathname<
next: NextFunction<InputContext | OutputContext>,
httpContext: HttpContext
) => {
if (!matchPathname(pathname, context.request.url.pathname)) {
if (context.status ?? !matchPathname(pathname, context.request.url.pathname)) {
return next(context);
}

View file

@ -0,0 +1,9 @@
import { createRouter } from '@withtyped/server';
const router = createRouter('/api')
.get('/status', {}, async (context, next) => next({ ...context, status: 204 }))
.get('/teapot', {}, async (context, next) =>
next({ ...context, status: 418, json: { message: 'The server refuses to brew coffee.' } })
);
export default router;