From 64f0c49847b34322a013dbd0379ee08d66ce8cf5 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 6 Apr 2024 16:44:04 +0800 Subject: [PATCH] Add type improvements in builders Include dynamic typing in builder functions. --- src/backend/core.ts | 4 ++-- src/common/app.ts | 29 +++++++++++++++++++++-------- src/common/charset.ts | 4 ++-- src/common/language.ts | 4 ++-- src/common/media-type.ts | 4 ++-- test/e2e/features.test.ts | 6 +++--- test/e2e/http.test.ts | 6 +++--- 7 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/backend/core.ts b/src/backend/core.ts index b8138b7..45e7081 100644 --- a/src/backend/core.ts +++ b/src/backend/core.ts @@ -3,7 +3,7 @@ import {createServer, CreateServerParams, Server} from './servers/http'; import {BackendState} from './common'; import {DataSource} from './data-source'; -export interface BackendBuilder { +export interface Backend { showTotalItemCountOnGetCollection(b?: boolean): this; showTotalItemCountOnCreateItem(b?: boolean): this; checksSerializersOnDelete(b?: boolean): this; @@ -52,5 +52,5 @@ export const createBackend = (params: CreateBackendParams) => { createHttpServer(serverParams = {} as CreateServerParams) { return createServer(backendState, serverParams); }, - } satisfies BackendBuilder; + } satisfies Backend; }; diff --git a/src/common/app.ts b/src/common/app.ts index bc012c1..04d7cc3 100644 --- a/src/common/app.ts +++ b/src/common/app.ts @@ -3,7 +3,7 @@ import {FALLBACK_LANGUAGE, Language} from './language'; import {FALLBACK_MEDIA_TYPE, MediaType} from './media-type'; import {Charset, FALLBACK_CHARSET} from './charset'; import * as v from 'valibot'; -import {BackendBuilder, createBackend, CreateBackendParams} from '../backend'; +import {Backend, createBackend, CreateBackendParams} from '../backend'; import {ClientBuilder, createClient, CreateClientParams} from '../client'; type ApplicationMap = Map; @@ -20,20 +20,33 @@ export interface ApplicationParams { name: string; } -export interface ApplicationBuilder { - mediaType(mediaType: MediaType): this; - language(language: Language): this; - charset(charset: Charset): this; +export interface Application< + Resources extends Resource[] = [], + MediaTypes extends MediaType[] = [], + Charsets extends Charset[] = [], + Languages extends Language[] = [] +> { + mediaType(mediaType: MediaType): Application< + Resources, [...MediaTypes, MediaType], Charsets, Languages + >; + language(language: Language): Application< + Resources, MediaTypes, Charsets, [...Languages, Language] + >; + charset(charset: Charset): Application< + Resources, MediaTypes, [...Charsets, Charset], Languages + >; resource< Schema extends v.BaseSchema, CurrentItemName extends string = string, CurrentRouteName extends string = string - >(resRaw: Resource): this; - createBackend(params: Omit): BackendBuilder; + >(resRaw: Resource): Application< + [...Resources, Resource], MediaTypes, Charsets, Languages + >; + createBackend(params: Omit): Backend; createClient(params: Omit): ClientBuilder; } -export const application = (appParams: ApplicationParams): ApplicationBuilder => { +export const application = (appParams: ApplicationParams): Application => { const appState: ApplicationState = { name: appParams.name, resources: new Set>(), diff --git a/src/common/charset.ts b/src/common/charset.ts index 7af75b2..e5086b4 100644 --- a/src/common/charset.ts +++ b/src/common/charset.ts @@ -1,5 +1,5 @@ -export interface Charset { - name: string; +export interface Charset { + name: Name; encode: (str: string) => Buffer; decode: (buf: Buffer) => string; } diff --git a/src/common/language.ts b/src/common/language.ts index cd6f262..52e5489 100644 --- a/src/common/language.ts +++ b/src/common/language.ts @@ -51,8 +51,8 @@ export type LanguageDefaultBodyKey = typeof LANGUAGE_DEFAULT_BODY_KEYS[number]; export interface LanguageBodyMap extends Record {} -export interface Language { - name: string, +export interface Language { + name: Name, statusMessages: LanguageStatusMessageMap, bodies: LanguageBodyMap } diff --git a/src/common/media-type.ts b/src/common/media-type.ts index b73302a..5d0b1f1 100644 --- a/src/common/media-type.ts +++ b/src/common/media-type.ts @@ -1,5 +1,5 @@ -export interface MediaType { - name: string; +export interface MediaType { + name: Name; serialize: (object: T) => string; deserialize: (s: string) => T; } diff --git a/test/e2e/features.test.ts b/test/e2e/features.test.ts index dcb7c41..fd20545 100644 --- a/test/e2e/features.test.ts +++ b/test/e2e/features.test.ts @@ -2,8 +2,8 @@ import {describe, afterAll, afterEach, beforeAll, beforeEach, it} from 'vitest'; import {mkdtemp, rm} from 'fs/promises'; import {join} from 'path'; import {tmpdir} from 'os'; -import {application, resource, Resource, validation as v} from '../../src/common'; -import {BackendBuilder, dataSources} from '../../src/backend'; +import {application, resource, Resource, ResourceType, validation as v} from '../../src/common'; +import {Backend, dataSources} from '../../src/backend'; import {autoIncrement} from '../fixtures'; import {RequestContext} from '../../src/backend/common'; @@ -52,7 +52,7 @@ describe('decorators', () => { }); }); - let server: ReturnType; + let server: ReturnType; beforeEach(() => { const app = application({ name: 'piano-service', diff --git a/test/e2e/http.test.ts b/test/e2e/http.test.ts index 935878e..b505b8e 100644 --- a/test/e2e/http.test.ts +++ b/test/e2e/http.test.ts @@ -20,7 +20,7 @@ import { } from 'path'; import {request} from 'http'; import {constants} from 'http2'; -import {BackendBuilder, dataSources} from '../../src/backend'; +import {Backend, dataSources} from '../../src/backend'; import { application, resource, validation as v, Resource } from '../../src/common'; import { autoIncrement } from '../fixtures'; @@ -69,8 +69,8 @@ describe('yasumi HTTP', () => { }); }); - let backend: BackendBuilder; - let server: ReturnType; + let backend: Backend; + let server: ReturnType; beforeEach(() => { const app = application({ name: 'piano-service',