From 615eb4ce5b0c74bdd9067966b41d71be73c21a07 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Thu, 23 May 2024 14:48:09 +0800 Subject: [PATCH] Update structure Organize exports. --- packages/core/src/backend/common.ts | 3 +-- packages/core/src/backend/server.ts | 2 +- packages/core/src/common/endpoint.ts | 2 +- packages/core/src/common/index.ts | 2 +- packages/core/src/common/recipe.ts | 2 +- packages/core/src/extenders/http/backend/core.ts | 16 ++++++++-------- packages/core/test/index.test.ts | 3 ++- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/core/src/backend/common.ts b/packages/core/src/backend/common.ts index 4a4d6a3..84a930c 100644 --- a/packages/core/src/backend/common.ts +++ b/packages/core/src/backend/common.ts @@ -1,5 +1,4 @@ -import {App as BaseApp, AppOperations, BaseAppState, Endpoint} from '../common'; -import {Response} from '../common/response'; +import {App as BaseApp, AppOperations, BaseAppState, Endpoint, Response} from '../common'; interface BackendParams { app: App; diff --git a/packages/core/src/backend/server.ts b/packages/core/src/backend/server.ts index 7d0fef0..585f4f4 100644 --- a/packages/core/src/backend/server.ts +++ b/packages/core/src/backend/server.ts @@ -1,5 +1,5 @@ -import {Backend as BaseBackend} from './common'; import {ServiceParams} from '../common'; +import {Backend as BaseBackend} from './common'; export interface ServerRequest {} diff --git a/packages/core/src/common/endpoint.ts b/packages/core/src/common/endpoint.ts index 4c30772..e1ef5c3 100644 --- a/packages/core/src/common/endpoint.ts +++ b/packages/core/src/common/endpoint.ts @@ -1,5 +1,5 @@ -import * as v from 'valibot'; import {DataSource} from './data-source'; +import {validation as v} from '.'; export type EndpointQueue = [Endpoint, Record | undefined][]; diff --git a/packages/core/src/common/index.ts b/packages/core/src/common/index.ts index b896adc..6f151cf 100644 --- a/packages/core/src/common/index.ts +++ b/packages/core/src/common/index.ts @@ -7,5 +7,5 @@ export * from './media-type'; export * from './operation'; export * from './response'; export * from './service'; -export * from './status-codes'; +export * as statusCodes from './status-codes'; export * as validation from 'valibot'; diff --git a/packages/core/src/common/recipe.ts b/packages/core/src/common/recipe.ts index baf9f26..114bdd4 100644 --- a/packages/core/src/common/recipe.ts +++ b/packages/core/src/common/recipe.ts @@ -1,6 +1,6 @@ +import {Backend} from '../backend'; import {Operation} from './operation'; import {App} from './app'; -import {Backend} from '../backend'; import {Endpoint} from './endpoint'; export interface RecipeState { diff --git a/packages/core/src/extenders/http/backend/core.ts b/packages/core/src/extenders/http/backend/core.ts index ae81f71..50a2277 100644 --- a/packages/core/src/extenders/http/backend/core.ts +++ b/packages/core/src/extenders/http/backend/core.ts @@ -7,7 +7,7 @@ import { ServerParams, } from '../../../backend'; import http from 'http'; -import { constants } from 'http2'; +import { statusCodes } from '../../../common'; declare module '../../../backend' { interface ServerRequest extends http.IncomingMessage {} @@ -26,12 +26,12 @@ class ServerInstance implements Server { private readonly requestListener = async (req: ServerRequest, res: ServerResponse) => { // const endpoints = this.backend.app.endpoints; if (typeof req.method === 'undefined') { - res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, {}); + res.writeHead(statusCodes.HTTP_STATUS_BAD_REQUEST, {}); res.end(); return; } if (typeof req.url === 'undefined') { - res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, {}); + res.writeHead(statusCodes.HTTP_STATUS_BAD_REQUEST, {}); res.end(); return; } @@ -44,7 +44,7 @@ class ServerInstance implements Server { .find((op) => op.method === req.method?.toUpperCase()); if (typeof foundAppOperation === 'undefined') { - res.writeHead(constants.HTTP_STATUS_METHOD_NOT_ALLOWED, { + res.writeHead(statusCodes.HTTP_STATUS_METHOD_NOT_ALLOWED, { 'Allow': appOperations.map((op) => op.method).join(',') }); res.end(); @@ -53,7 +53,7 @@ class ServerInstance implements Server { const endpointOperations = Array.from(endpoint?.operations ?? []); if (!endpointOperations.includes(foundAppOperation.name)) { - res.writeHead(constants.HTTP_STATUS_METHOD_NOT_ALLOWED, { + res.writeHead(statusCodes.HTTP_STATUS_METHOD_NOT_ALLOWED, { 'Allow': endpointOperations .map((a) => appOperations.find((aa) => aa.name === a)?.method) .join(',') @@ -64,13 +64,13 @@ class ServerInstance implements Server { const implementation = this.backend.implementations.get(foundAppOperation.name); if (typeof implementation === 'undefined') { - res.writeHead(constants.HTTP_STATUS_NOT_IMPLEMENTED); + res.writeHead(statusCodes.HTTP_STATUS_NOT_IMPLEMENTED); res.end(); return; } if (typeof endpoint === 'undefined') { - res.writeHead(constants.HTTP_STATUS_NOT_IMPLEMENTED); + res.writeHead(statusCodes.HTTP_STATUS_NOT_IMPLEMENTED); res.end(); return; } @@ -88,7 +88,7 @@ class ServerInstance implements Server { }); if (typeof responseSpec === 'undefined') { - res.writeHead(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY, {}); + res.writeHead(statusCodes.HTTP_STATUS_UNPROCESSABLE_ENTITY, {}); res.end(); return; } diff --git a/packages/core/test/index.test.ts b/packages/core/test/index.test.ts index f69470a..b3c896d 100644 --- a/packages/core/test/index.test.ts +++ b/packages/core/test/index.test.ts @@ -7,6 +7,7 @@ import { endpoint, Operation, operation, + statusCodes, validation as v, } from '../src/common'; import {Backend, backend, Server} from '../src/backend'; @@ -99,7 +100,7 @@ describe('app', () => { .at(theEndpoint, { resourceId: 3 }) .makeRequest(theOperation); - expect(response).toHaveProperty('status', 422); + expect(response).toHaveProperty('status', statusCodes.HTTP_STATUS_UNPROCESSABLE_ENTITY); }); });