diff --git a/TODO.md b/TODO.md index 13a05d6..ad4bd87 100644 --- a/TODO.md +++ b/TODO.md @@ -37,3 +37,5 @@ - [ ] Plugin support - [ ] Add option to reject content negotiation params with `406 Not Acceptable` - [ ] Add `fast-check` for property-based checking +- [X] Split HTTP server from backend core + - [ ] Add HTTP entrypoint (package?) diff --git a/packages/core/src/backend/common.ts b/packages/core/src/backend/common.ts index f31da14..80cfdec 100644 --- a/packages/core/src/backend/common.ts +++ b/packages/core/src/backend/common.ts @@ -77,7 +77,7 @@ export interface Backend { checksSerializersOnDelete(b?: boolean): this; throwsErrorOnDeletingNotFound(b?: boolean): this; use(extender: (state: BackendState, t: this) => BackendExtended): BackendExtended; - createServer(type: string, options?: {}): T; + createServer(type: string, options?: {}): T; dataSource?: (resource: Resource) => T; } diff --git a/packages/core/src/backend/core.ts b/packages/core/src/backend/core.ts index b63b188..7b5155f 100644 --- a/packages/core/src/backend/core.ts +++ b/packages/core/src/backend/core.ts @@ -43,7 +43,7 @@ export const createBackend = (params: CreateBackendParams) => { backendState.checksSerializersOnDelete = b; return this; }, - createServer(): Server { + createServer(_type: string, _options = {}): Server { return { requestDecorator() { return this; diff --git a/packages/core/src/backend/index.ts b/packages/core/src/backend/index.ts index 1243f85..126256a 100644 --- a/packages/core/src/backend/index.ts +++ b/packages/core/src/backend/index.ts @@ -1,3 +1,6 @@ export * from './core'; export * from './common'; export * from './data-source'; + +// where to put these? we should be publishing them as a separate entry point +export * as http from '../servers/http'; diff --git a/packages/core/src/common/resource.ts b/packages/core/src/common/resource.ts index 6dc8f86..424aec4 100644 --- a/packages/core/src/common/resource.ts +++ b/packages/core/src/common/resource.ts @@ -1,6 +1,6 @@ import * as v from 'valibot'; import {PatchContentType} from './media-type'; -import {DataSource, ResourceIdConfig} from '../backend'; +import {DataSource, ResourceIdConfig} from '../backend/data-source'; export const CAN_PATCH_VALID_VALUES = ['merge', 'delta'] as const; diff --git a/packages/core/src/servers/http/core.ts b/packages/core/src/servers/http/core.ts index 7d80dfd..7371eee 100644 --- a/packages/core/src/servers/http/core.ts +++ b/packages/core/src/servers/http/core.ts @@ -12,8 +12,10 @@ import { RequestDecorator, Response, Server, - DataSource, -} from '../../backend'; +} from '../../backend/common'; +import { + DataSource +} from '../../backend/data-source'; import { BaseResourceType, CanPatchSpec, diff --git a/packages/core/src/servers/http/decorators/backend/content-negotiation.ts b/packages/core/src/servers/http/decorators/backend/content-negotiation.ts index 4c6a9f0..dcffaa7 100644 --- a/packages/core/src/servers/http/decorators/backend/content-negotiation.ts +++ b/packages/core/src/servers/http/decorators/backend/content-negotiation.ts @@ -1,8 +1,8 @@ import {ContentNegotiation} from '../../../../common'; -import {RequestDecorator} from '../../../../backend'; +import {RequestDecorator} from '../../../../backend/common'; import Negotiator from 'negotiator'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { cn: Partial; } diff --git a/packages/core/src/servers/http/decorators/backend/index.ts b/packages/core/src/servers/http/decorators/backend/index.ts index 94f7406..a20178e 100644 --- a/packages/core/src/servers/http/decorators/backend/index.ts +++ b/packages/core/src/servers/http/decorators/backend/index.ts @@ -1,8 +1,8 @@ -import {BackendState, ParamRequestDecorator} from '../../../../backend'; +import {BackendState, ParamRequestDecorator} from '../../../../backend/common'; import {decorateRequestWithContentNegotiation} from './content-negotiation'; import {decorateRequestWithResource} from './resource'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { backend: BackendState; } diff --git a/packages/core/src/servers/http/decorators/backend/resource.ts b/packages/core/src/servers/http/decorators/backend/resource.ts index 353b0f5..7ccfc32 100644 --- a/packages/core/src/servers/http/decorators/backend/resource.ts +++ b/packages/core/src/servers/http/decorators/backend/resource.ts @@ -1,7 +1,7 @@ import {Resource} from '../../../../common'; -import {RequestDecorator} from '../../../../backend'; +import {RequestDecorator} from '../../../../backend/common'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { resource?: Resource; resourceId?: string; diff --git a/packages/core/src/servers/http/decorators/method/index.ts b/packages/core/src/servers/http/decorators/method/index.ts index 960ea0e..f39778a 100644 --- a/packages/core/src/servers/http/decorators/method/index.ts +++ b/packages/core/src/servers/http/decorators/method/index.ts @@ -1,4 +1,4 @@ -import {RequestDecorator} from '../../../../backend'; +import {RequestDecorator} from '../../../../backend/common'; const METHOD_SPOOF_HEADER_NAME = 'x-original-method' as const; const METHOD_SPOOF_ORIGINAL_METHOD = 'POST' as const; diff --git a/packages/core/src/servers/http/decorators/url/base-path.ts b/packages/core/src/servers/http/decorators/url/base-path.ts index 507dfbb..7a0274c 100644 --- a/packages/core/src/servers/http/decorators/url/base-path.ts +++ b/packages/core/src/servers/http/decorators/url/base-path.ts @@ -1,6 +1,6 @@ -import {ParamRequestDecorator} from '../../../../backend'; +import {ParamRequestDecorator} from '../../../../backend/common'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { basePath: string; } diff --git a/packages/core/src/servers/http/decorators/url/host.ts b/packages/core/src/servers/http/decorators/url/host.ts index fe79483..465dc84 100644 --- a/packages/core/src/servers/http/decorators/url/host.ts +++ b/packages/core/src/servers/http/decorators/url/host.ts @@ -1,6 +1,6 @@ -import {ParamRequestDecorator} from '../../../../backend'; +import {ParamRequestDecorator} from '../../../../backend/common'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { host: string; } diff --git a/packages/core/src/servers/http/decorators/url/index.ts b/packages/core/src/servers/http/decorators/url/index.ts index a92abcf..efe4f0c 100644 --- a/packages/core/src/servers/http/decorators/url/index.ts +++ b/packages/core/src/servers/http/decorators/url/index.ts @@ -1,10 +1,10 @@ -import {ParamRequestDecorator} from '../../../../backend'; +import {ParamRequestDecorator} from '../../../../backend/common'; import {CreateServerParams} from '../../core'; import {decorateRequestWithScheme} from './scheme'; import {decorateRequestWithHost} from './host'; import {decorateRequestWithBasePath} from './base-path'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { rawUrl?: string; query: URLSearchParams; diff --git a/packages/core/src/servers/http/decorators/url/scheme.ts b/packages/core/src/servers/http/decorators/url/scheme.ts index 5729490..3e92c70 100644 --- a/packages/core/src/servers/http/decorators/url/scheme.ts +++ b/packages/core/src/servers/http/decorators/url/scheme.ts @@ -1,6 +1,6 @@ -import {ParamRequestDecorator} from '../../../../backend'; +import {ParamRequestDecorator} from '../../../../backend/common'; -declare module '../../../../backend' { +declare module '../../../../backend/common' { interface RequestContext { scheme: string; } diff --git a/packages/core/src/servers/http/handlers/default.ts b/packages/core/src/servers/http/handlers/default.ts index 5e0bebc..2717c2e 100644 --- a/packages/core/src/servers/http/handlers/default.ts +++ b/packages/core/src/servers/http/handlers/default.ts @@ -1,5 +1,5 @@ import {constants} from 'http2'; -import {AllowedMiddlewareSpecification, getAllowString, Middleware} from '../../../backend'; +import {AllowedMiddlewareSpecification, getAllowString, Middleware} from '../../../backend/common'; import {LinkMap} from '../utils'; import {PlainResponse, ErrorPlainResponse} from '../response'; import {getAcceptPatchString, getAcceptPostString} from '../../../common'; diff --git a/packages/core/src/servers/http/handlers/resource.ts b/packages/core/src/servers/http/handlers/resource.ts index 8427511..d5a8524 100644 --- a/packages/core/src/servers/http/handlers/resource.ts +++ b/packages/core/src/servers/http/handlers/resource.ts @@ -1,7 +1,7 @@ import { constants } from 'http2'; import * as v from 'valibot'; import assert from 'assert'; -import {Middleware} from '../../../backend'; +import {Middleware} from '../../../backend/common'; import { applyDelta, Query, Delta, diff --git a/packages/core/src/servers/http/response.ts b/packages/core/src/servers/http/response.ts index ad0ef7c..f38218d 100644 --- a/packages/core/src/servers/http/response.ts +++ b/packages/core/src/servers/http/response.ts @@ -1,5 +1,5 @@ import {Language, LanguageStatusMessageMap} from '../../common'; -import {MiddlewareResponseError, Response} from '../../backend'; +import {MiddlewareResponseError, Response} from '../../backend/common'; interface PlainResponseParams extends Response { body?: T; diff --git a/packages/core/test/features/decorators.test.ts b/packages/core/test/features/decorators.test.ts index 4582ea4..e93b9ac 100644 --- a/packages/core/test/features/decorators.test.ts +++ b/packages/core/test/features/decorators.test.ts @@ -1,6 +1,6 @@ import {describe, afterAll, beforeAll, it} from 'vitest'; import {Application, application, resource, Resource, validation as v} from '../../src/common'; -import {Backend, DataSource, RequestContext, Server} from '../../src/backend'; +import {Backend, DataSource, RequestContext} from '../../src/backend'; import {createTestClient, DummyDataSource, dummyGenerationStrategy, TEST_LANGUAGE, TestClient} from '../utils'; import {httpExtender, HttpServer} from '../../src/servers/http'; diff --git a/packages/examples/cms-web-api/src/index.ts b/packages/examples/cms-web-api/src/index.ts index 284a5d9..05ec750 100644 --- a/packages/examples/cms-web-api/src/index.ts +++ b/packages/examples/cms-web-api/src/index.ts @@ -1,7 +1,7 @@ import { application, resource, validation as v } from '@modal-sh/yasumi'; import { http } from '@modal-sh/yasumi/backend'; import { randomUUID } from 'crypto'; -import {JsonLinesDataSource} from '@modal-sh/yasumi-data-source-file-jsonl'; +import { JsonLinesDataSource } from '@modal-sh/yasumi-data-source-file-jsonl'; import { constants } from 'http2'; import TAGALOG from './languages/tl'; @@ -17,8 +17,8 @@ const User = resource( v.object({ email: v.string([v.email()]), password: v.string(), - createdAt: v.datelike(), - updatedAt: v.datelike(), + createdAt: v.date(), + updatedAt: v.date(), }) ) .name('User') @@ -59,9 +59,10 @@ const app = application({ const backend = app.createBackend({ dataSource: new JsonLinesDataSource(), }) + .use(http.httpExtender) .throwsErrorOnDeletingNotFound(); -const server = backend.createHttpServer({ +const server = backend.createServer('http', { basePath: '/api', }) .defaultErrorHandler((_req, res) => () => { diff --git a/packages/examples/cms-web-api/src/languages/tl.ts b/packages/examples/cms-web-api/src/languages/tl.ts index f80bbcc..b096fb5 100644 --- a/packages/examples/cms-web-api/src/languages/tl.ts +++ b/packages/examples/cms-web-api/src/languages/tl.ts @@ -20,6 +20,7 @@ export default { badRequest: 'Maling Paghiling', ok: 'OK', provideOptions: 'Magbigay ng mga Pagpipilian', + resourceCollectionQueried: 'Hinanapan ang $RESOURCE Collection', resourceCollectionFetched: 'Nakuha ang $RESOURCE Collection', resourceFetched: 'Nakuha ang $RESOURCE', resourceNotFound: 'Hindi Nahanap ang $RESOURCE',