Browse Source

Update interface augmentations

Use correct paths when referring to extended interfaces.
master
TheoryOfNekomata 6 months ago
parent
commit
a10c4baff8
20 changed files with 37 additions and 28 deletions
  1. +2
    -0
      TODO.md
  2. +1
    -1
      packages/core/src/backend/common.ts
  3. +1
    -1
      packages/core/src/backend/core.ts
  4. +3
    -0
      packages/core/src/backend/index.ts
  5. +1
    -1
      packages/core/src/common/resource.ts
  6. +4
    -2
      packages/core/src/servers/http/core.ts
  7. +2
    -2
      packages/core/src/servers/http/decorators/backend/content-negotiation.ts
  8. +2
    -2
      packages/core/src/servers/http/decorators/backend/index.ts
  9. +2
    -2
      packages/core/src/servers/http/decorators/backend/resource.ts
  10. +1
    -1
      packages/core/src/servers/http/decorators/method/index.ts
  11. +2
    -2
      packages/core/src/servers/http/decorators/url/base-path.ts
  12. +2
    -2
      packages/core/src/servers/http/decorators/url/host.ts
  13. +2
    -2
      packages/core/src/servers/http/decorators/url/index.ts
  14. +2
    -2
      packages/core/src/servers/http/decorators/url/scheme.ts
  15. +1
    -1
      packages/core/src/servers/http/handlers/default.ts
  16. +1
    -1
      packages/core/src/servers/http/handlers/resource.ts
  17. +1
    -1
      packages/core/src/servers/http/response.ts
  18. +1
    -1
      packages/core/test/features/decorators.test.ts
  19. +5
    -4
      packages/examples/cms-web-api/src/index.ts
  20. +1
    -0
      packages/examples/cms-web-api/src/languages/tl.ts

+ 2
- 0
TODO.md View File

@@ -37,3 +37,5 @@
- [ ] Plugin support - [ ] Plugin support
- [ ] Add option to reject content negotiation params with `406 Not Acceptable` - [ ] Add option to reject content negotiation params with `406 Not Acceptable`
- [ ] Add `fast-check` for property-based checking - [ ] Add `fast-check` for property-based checking
- [X] Split HTTP server from backend core
- [ ] Add HTTP entrypoint (package?)

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

@@ -77,7 +77,7 @@ export interface Backend<T extends DataSource = DataSource> {
checksSerializersOnDelete(b?: boolean): this; checksSerializersOnDelete(b?: boolean): this;
throwsErrorOnDeletingNotFound(b?: boolean): this; throwsErrorOnDeletingNotFound(b?: boolean): this;
use<BackendExtended extends this>(extender: (state: BackendState, t: this) => BackendExtended): BackendExtended; use<BackendExtended extends this>(extender: (state: BackendState, t: this) => BackendExtended): BackendExtended;
createServer<T extends Server>(type: string, options?: {}): T;
createServer<T extends Server = Server>(type: string, options?: {}): T;
dataSource?: (resource: Resource) => T; dataSource?: (resource: Resource) => T;
} }




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

@@ -43,7 +43,7 @@ export const createBackend = (params: CreateBackendParams) => {
backendState.checksSerializersOnDelete = b; backendState.checksSerializersOnDelete = b;
return this; return this;
}, },
createServer(): Server {
createServer(_type: string, _options = {}): Server {
return { return {
requestDecorator() { requestDecorator() {
return this; return this;


+ 3
- 0
packages/core/src/backend/index.ts View File

@@ -1,3 +1,6 @@
export * from './core'; export * from './core';
export * from './common'; export * from './common';
export * from './data-source'; export * from './data-source';

// where to put these? we should be publishing them as a separate entry point
export * as http from '../servers/http';

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

@@ -1,6 +1,6 @@
import * as v from 'valibot'; import * as v from 'valibot';
import {PatchContentType} from './media-type'; 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; export const CAN_PATCH_VALID_VALUES = ['merge', 'delta'] as const;




+ 4
- 2
packages/core/src/servers/http/core.ts View File

@@ -12,8 +12,10 @@ import {
RequestDecorator, RequestDecorator,
Response, Response,
Server, Server,
DataSource,
} from '../../backend';
} from '../../backend/common';
import {
DataSource
} from '../../backend/data-source';
import { import {
BaseResourceType, BaseResourceType,
CanPatchSpec, CanPatchSpec,


+ 2
- 2
packages/core/src/servers/http/decorators/backend/content-negotiation.ts View File

@@ -1,8 +1,8 @@
import {ContentNegotiation} from '../../../../common'; import {ContentNegotiation} from '../../../../common';
import {RequestDecorator} from '../../../../backend';
import {RequestDecorator} from '../../../../backend/common';
import Negotiator from 'negotiator'; import Negotiator from 'negotiator';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
cn: Partial<ContentNegotiation>; cn: Partial<ContentNegotiation>;
} }


+ 2
- 2
packages/core/src/servers/http/decorators/backend/index.ts View File

@@ -1,8 +1,8 @@
import {BackendState, ParamRequestDecorator} from '../../../../backend';
import {BackendState, ParamRequestDecorator} from '../../../../backend/common';
import {decorateRequestWithContentNegotiation} from './content-negotiation'; import {decorateRequestWithContentNegotiation} from './content-negotiation';
import {decorateRequestWithResource} from './resource'; import {decorateRequestWithResource} from './resource';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
backend: BackendState; backend: BackendState;
} }


+ 2
- 2
packages/core/src/servers/http/decorators/backend/resource.ts View File

@@ -1,7 +1,7 @@
import {Resource} from '../../../../common'; import {Resource} from '../../../../common';
import {RequestDecorator} from '../../../../backend';
import {RequestDecorator} from '../../../../backend/common';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
resource?: Resource; resource?: Resource;
resourceId?: string; resourceId?: string;


+ 1
- 1
packages/core/src/servers/http/decorators/method/index.ts View File

@@ -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_HEADER_NAME = 'x-original-method' as const;
const METHOD_SPOOF_ORIGINAL_METHOD = 'POST' as const; const METHOD_SPOOF_ORIGINAL_METHOD = 'POST' as const;


+ 2
- 2
packages/core/src/servers/http/decorators/url/base-path.ts View File

@@ -1,6 +1,6 @@
import {ParamRequestDecorator} from '../../../../backend';
import {ParamRequestDecorator} from '../../../../backend/common';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
basePath: string; basePath: string;
} }


+ 2
- 2
packages/core/src/servers/http/decorators/url/host.ts View File

@@ -1,6 +1,6 @@
import {ParamRequestDecorator} from '../../../../backend';
import {ParamRequestDecorator} from '../../../../backend/common';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
host: string; host: string;
} }


+ 2
- 2
packages/core/src/servers/http/decorators/url/index.ts View File

@@ -1,10 +1,10 @@
import {ParamRequestDecorator} from '../../../../backend';
import {ParamRequestDecorator} from '../../../../backend/common';
import {CreateServerParams} from '../../core'; import {CreateServerParams} from '../../core';
import {decorateRequestWithScheme} from './scheme'; import {decorateRequestWithScheme} from './scheme';
import {decorateRequestWithHost} from './host'; import {decorateRequestWithHost} from './host';
import {decorateRequestWithBasePath} from './base-path'; import {decorateRequestWithBasePath} from './base-path';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
rawUrl?: string; rawUrl?: string;
query: URLSearchParams; query: URLSearchParams;


+ 2
- 2
packages/core/src/servers/http/decorators/url/scheme.ts View File

@@ -1,6 +1,6 @@
import {ParamRequestDecorator} from '../../../../backend';
import {ParamRequestDecorator} from '../../../../backend/common';


declare module '../../../../backend' {
declare module '../../../../backend/common' {
interface RequestContext { interface RequestContext {
scheme: string; scheme: string;
} }


+ 1
- 1
packages/core/src/servers/http/handlers/default.ts View File

@@ -1,5 +1,5 @@
import {constants} from 'http2'; import {constants} from 'http2';
import {AllowedMiddlewareSpecification, getAllowString, Middleware} from '../../../backend';
import {AllowedMiddlewareSpecification, getAllowString, Middleware} from '../../../backend/common';
import {LinkMap} from '../utils'; import {LinkMap} from '../utils';
import {PlainResponse, ErrorPlainResponse} from '../response'; import {PlainResponse, ErrorPlainResponse} from '../response';
import {getAcceptPatchString, getAcceptPostString} from '../../../common'; import {getAcceptPatchString, getAcceptPostString} from '../../../common';


+ 1
- 1
packages/core/src/servers/http/handlers/resource.ts View File

@@ -1,7 +1,7 @@
import { constants } from 'http2'; import { constants } from 'http2';
import * as v from 'valibot'; import * as v from 'valibot';
import assert from 'assert'; import assert from 'assert';
import {Middleware} from '../../../backend';
import {Middleware} from '../../../backend/common';
import { import {
applyDelta, Query, applyDelta, Query,
Delta, Delta,


+ 1
- 1
packages/core/src/servers/http/response.ts View File

@@ -1,5 +1,5 @@
import {Language, LanguageStatusMessageMap} from '../../common'; import {Language, LanguageStatusMessageMap} from '../../common';
import {MiddlewareResponseError, Response} from '../../backend';
import {MiddlewareResponseError, Response} from '../../backend/common';


interface PlainResponseParams<T = unknown, U extends NodeJS.EventEmitter = NodeJS.EventEmitter> extends Response { interface PlainResponseParams<T = unknown, U extends NodeJS.EventEmitter = NodeJS.EventEmitter> extends Response {
body?: T; body?: T;


+ 1
- 1
packages/core/test/features/decorators.test.ts View File

@@ -1,6 +1,6 @@
import {describe, afterAll, beforeAll, it} from 'vitest'; import {describe, afterAll, beforeAll, it} from 'vitest';
import {Application, application, resource, Resource, validation as v} from '../../src/common'; 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 {createTestClient, DummyDataSource, dummyGenerationStrategy, TEST_LANGUAGE, TestClient} from '../utils';
import {httpExtender, HttpServer} from '../../src/servers/http'; import {httpExtender, HttpServer} from '../../src/servers/http';




+ 5
- 4
packages/examples/cms-web-api/src/index.ts View File

@@ -1,7 +1,7 @@
import { application, resource, validation as v } from '@modal-sh/yasumi'; import { application, resource, validation as v } from '@modal-sh/yasumi';
import { http } from '@modal-sh/yasumi/backend'; import { http } from '@modal-sh/yasumi/backend';
import { randomUUID } from 'crypto'; 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 { constants } from 'http2';
import TAGALOG from './languages/tl'; import TAGALOG from './languages/tl';


@@ -17,8 +17,8 @@ const User = resource(
v.object({ v.object({
email: v.string([v.email()]), email: v.string([v.email()]),
password: v.string(), password: v.string(),
createdAt: v.datelike(),
updatedAt: v.datelike(),
createdAt: v.date(),
updatedAt: v.date(),
}) })
) )
.name('User') .name('User')
@@ -59,9 +59,10 @@ const app = application({
const backend = app.createBackend({ const backend = app.createBackend({
dataSource: new JsonLinesDataSource(), dataSource: new JsonLinesDataSource(),
}) })
.use(http.httpExtender)
.throwsErrorOnDeletingNotFound(); .throwsErrorOnDeletingNotFound();


const server = backend.createHttpServer({
const server = backend.createServer('http', {
basePath: '/api', basePath: '/api',
}) })
.defaultErrorHandler((_req, res) => () => { .defaultErrorHandler((_req, res) => () => {


+ 1
- 0
packages/examples/cms-web-api/src/languages/tl.ts View File

@@ -20,6 +20,7 @@ export default {
badRequest: 'Maling Paghiling', badRequest: 'Maling Paghiling',
ok: 'OK', ok: 'OK',
provideOptions: 'Magbigay ng mga Pagpipilian', provideOptions: 'Magbigay ng mga Pagpipilian',
resourceCollectionQueried: 'Hinanapan ang $RESOURCE Collection',
resourceCollectionFetched: 'Nakuha ang $RESOURCE Collection', resourceCollectionFetched: 'Nakuha ang $RESOURCE Collection',
resourceFetched: 'Nakuha ang $RESOURCE', resourceFetched: 'Nakuha ang $RESOURCE',
resourceNotFound: 'Hindi Nahanap ang $RESOURCE', resourceNotFound: 'Hindi Nahanap ang $RESOURCE',


Loading…
Cancel
Save