Browse Source

Update structure

Organize exports.
refactor/new-arch
TheoryOfNekomata 6 months ago
parent
commit
615eb4ce5b
7 changed files with 15 additions and 15 deletions
  1. +1
    -2
      packages/core/src/backend/common.ts
  2. +1
    -1
      packages/core/src/backend/server.ts
  3. +1
    -1
      packages/core/src/common/endpoint.ts
  4. +1
    -1
      packages/core/src/common/index.ts
  5. +1
    -1
      packages/core/src/common/recipe.ts
  6. +8
    -8
      packages/core/src/extenders/http/backend/core.ts
  7. +2
    -1
      packages/core/test/index.test.ts

+ 1
- 2
packages/core/src/backend/common.ts View File

@@ -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 extends BaseApp> { interface BackendParams<App extends BaseApp> {
app: App; app: App;


+ 1
- 1
packages/core/src/backend/server.ts View File

@@ -1,5 +1,5 @@
import {Backend as BaseBackend} from './common';
import {ServiceParams} from '../common'; import {ServiceParams} from '../common';
import {Backend as BaseBackend} from './common';


export interface ServerRequest {} export interface ServerRequest {}




+ 1
- 1
packages/core/src/common/endpoint.ts View File

@@ -1,5 +1,5 @@
import * as v from 'valibot';
import {DataSource} from './data-source'; import {DataSource} from './data-source';
import {validation as v} from '.';


export type EndpointQueue = [Endpoint, Record<string, unknown> | undefined][]; export type EndpointQueue = [Endpoint, Record<string, unknown> | undefined][];




+ 1
- 1
packages/core/src/common/index.ts View File

@@ -7,5 +7,5 @@ export * from './media-type';
export * from './operation'; export * from './operation';
export * from './response'; export * from './response';
export * from './service'; export * from './service';
export * from './status-codes';
export * as statusCodes from './status-codes';
export * as validation from 'valibot'; export * as validation from 'valibot';

+ 1
- 1
packages/core/src/common/recipe.ts View File

@@ -1,6 +1,6 @@
import {Backend} from '../backend';
import {Operation} from './operation'; import {Operation} from './operation';
import {App} from './app'; import {App} from './app';
import {Backend} from '../backend';
import {Endpoint} from './endpoint'; import {Endpoint} from './endpoint';


export interface RecipeState<A extends App = App> { export interface RecipeState<A extends App = App> {


+ 8
- 8
packages/core/src/extenders/http/backend/core.ts View File

@@ -7,7 +7,7 @@ import {
ServerParams, ServerParams,
} from '../../../backend'; } from '../../../backend';
import http from 'http'; import http from 'http';
import { constants } from 'http2';
import { statusCodes } from '../../../common';


declare module '../../../backend' { declare module '../../../backend' {
interface ServerRequest extends http.IncomingMessage {} interface ServerRequest extends http.IncomingMessage {}
@@ -26,12 +26,12 @@ class ServerInstance<Backend extends BaseBackend> implements Server<Backend> {
private readonly requestListener = async (req: ServerRequest, res: ServerResponse) => { private readonly requestListener = async (req: ServerRequest, res: ServerResponse) => {
// const endpoints = this.backend.app.endpoints; // const endpoints = this.backend.app.endpoints;
if (typeof req.method === 'undefined') { if (typeof req.method === 'undefined') {
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, {});
res.writeHead(statusCodes.HTTP_STATUS_BAD_REQUEST, {});
res.end(); res.end();
return; return;
} }
if (typeof req.url === 'undefined') { if (typeof req.url === 'undefined') {
res.writeHead(constants.HTTP_STATUS_BAD_REQUEST, {});
res.writeHead(statusCodes.HTTP_STATUS_BAD_REQUEST, {});
res.end(); res.end();
return; return;
} }
@@ -44,7 +44,7 @@ class ServerInstance<Backend extends BaseBackend> implements Server<Backend> {
.find((op) => op.method === req.method?.toUpperCase()); .find((op) => op.method === req.method?.toUpperCase());


if (typeof foundAppOperation === 'undefined') { 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(',') 'Allow': appOperations.map((op) => op.method).join(',')
}); });
res.end(); res.end();
@@ -53,7 +53,7 @@ class ServerInstance<Backend extends BaseBackend> implements Server<Backend> {


const endpointOperations = Array.from(endpoint?.operations ?? []); const endpointOperations = Array.from(endpoint?.operations ?? []);
if (!endpointOperations.includes(foundAppOperation.name)) { if (!endpointOperations.includes(foundAppOperation.name)) {
res.writeHead(constants.HTTP_STATUS_METHOD_NOT_ALLOWED, {
res.writeHead(statusCodes.HTTP_STATUS_METHOD_NOT_ALLOWED, {
'Allow': endpointOperations 'Allow': endpointOperations
.map((a) => appOperations.find((aa) => aa.name === a)?.method) .map((a) => appOperations.find((aa) => aa.name === a)?.method)
.join(',') .join(',')
@@ -64,13 +64,13 @@ class ServerInstance<Backend extends BaseBackend> implements Server<Backend> {


const implementation = this.backend.implementations.get(foundAppOperation.name); const implementation = this.backend.implementations.get(foundAppOperation.name);
if (typeof implementation === 'undefined') { if (typeof implementation === 'undefined') {
res.writeHead(constants.HTTP_STATUS_NOT_IMPLEMENTED);
res.writeHead(statusCodes.HTTP_STATUS_NOT_IMPLEMENTED);
res.end(); res.end();
return; return;
} }


if (typeof endpoint === 'undefined') { if (typeof endpoint === 'undefined') {
res.writeHead(constants.HTTP_STATUS_NOT_IMPLEMENTED);
res.writeHead(statusCodes.HTTP_STATUS_NOT_IMPLEMENTED);
res.end(); res.end();
return; return;
} }
@@ -88,7 +88,7 @@ class ServerInstance<Backend extends BaseBackend> implements Server<Backend> {
}); });


if (typeof responseSpec === 'undefined') { if (typeof responseSpec === 'undefined') {
res.writeHead(constants.HTTP_STATUS_UNPROCESSABLE_ENTITY, {});
res.writeHead(statusCodes.HTTP_STATUS_UNPROCESSABLE_ENTITY, {});
res.end(); res.end();
return; return;
} }


+ 2
- 1
packages/core/test/index.test.ts View File

@@ -7,6 +7,7 @@ import {
endpoint, endpoint,
Operation, Operation,
operation, operation,
statusCodes,
validation as v, validation as v,
} from '../src/common'; } from '../src/common';
import {Backend, backend, Server} from '../src/backend'; import {Backend, backend, Server} from '../src/backend';
@@ -99,7 +100,7 @@ describe('app', () => {
.at(theEndpoint, { resourceId: 3 }) .at(theEndpoint, { resourceId: 3 })
.makeRequest(theOperation); .makeRequest(theOperation);


expect(response).toHaveProperty('status', 422);
expect(response).toHaveProperty('status', statusCodes.HTTP_STATUS_UNPROCESSABLE_ENTITY);
}); });
}); });




Loading…
Cancel
Save