Browse Source

Add type improvements in builders

Include dynamic typing in builder functions.
master
TheoryOfNekomata 7 months ago
parent
commit
64f0c49847
7 changed files with 35 additions and 22 deletions
  1. +2
    -2
      src/backend/core.ts
  2. +21
    -8
      src/common/app.ts
  3. +2
    -2
      src/common/charset.ts
  4. +2
    -2
      src/common/language.ts
  5. +2
    -2
      src/common/media-type.ts
  6. +3
    -3
      test/e2e/features.test.ts
  7. +3
    -3
      test/e2e/http.test.ts

+ 2
- 2
src/backend/core.ts View File

@@ -3,7 +3,7 @@ import {createServer, CreateServerParams, Server} from './servers/http';
import {BackendState} from './common';
import {DataSource} from './data-source';

export interface BackendBuilder<T extends DataSource = DataSource> {
export interface Backend<T extends DataSource = DataSource> {
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;
};

+ 21
- 8
src/common/app.ts View File

@@ -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<T extends { name: string }> = Map<T['name'], T>;
@@ -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<MediaTypeName extends string = string>(mediaType: MediaType<MediaTypeName>): Application<
Resources, [...MediaTypes, MediaType<MediaTypeName>], Charsets, Languages
>;
language<LanguageName extends string = string>(language: Language<LanguageName>): Application<
Resources, MediaTypes, Charsets, [...Languages, Language<LanguageName>]
>;
charset<CharsetName extends string = string>(charset: Charset<CharsetName>): Application<
Resources, MediaTypes, [...Charsets, Charset<CharsetName>], Languages
>;
resource<
Schema extends v.BaseSchema,
CurrentItemName extends string = string,
CurrentRouteName extends string = string
>(resRaw: Resource<Schema, CurrentItemName, CurrentRouteName>): this;
createBackend(params: Omit<CreateBackendParams, 'app'>): BackendBuilder;
>(resRaw: Resource<Schema, CurrentItemName, CurrentRouteName>): Application<
[...Resources, Resource<Schema, CurrentItemName, CurrentRouteName>], MediaTypes, Charsets, Languages
>;
createBackend(params: Omit<CreateBackendParams, 'app'>): Backend;
createClient(params: Omit<CreateClientParams, 'app'>): ClientBuilder;
}

export const application = (appParams: ApplicationParams): ApplicationBuilder => {
export const application = (appParams: ApplicationParams): Application => {
const appState: ApplicationState = {
name: appParams.name,
resources: new Set<Resource<any>>(),


+ 2
- 2
src/common/charset.ts View File

@@ -1,5 +1,5 @@
export interface Charset {
name: string;
export interface Charset<Name extends string = string> {
name: Name;
encode: (str: string) => Buffer;
decode: (buf: Buffer) => string;
}


+ 2
- 2
src/common/language.ts View File

@@ -51,8 +51,8 @@ export type LanguageDefaultBodyKey = typeof LANGUAGE_DEFAULT_BODY_KEYS[number];

export interface LanguageBodyMap extends Record<LanguageDefaultBodyKey, MessageBody> {}

export interface Language {
name: string,
export interface Language<Name extends string = string> {
name: Name,
statusMessages: LanguageStatusMessageMap,
bodies: LanguageBodyMap
}


+ 2
- 2
src/common/media-type.ts View File

@@ -1,5 +1,5 @@
export interface MediaType {
name: string;
export interface MediaType<Name extends string = string> {
name: Name;
serialize: <T>(object: T) => string;
deserialize: <T>(s: string) => T;
}


+ 3
- 3
test/e2e/features.test.ts View File

@@ -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<BackendBuilder['createHttpServer']>;
let server: ReturnType<Backend['createHttpServer']>;
beforeEach(() => {
const app = application({
name: 'piano-service',


+ 3
- 3
test/e2e/http.test.ts View File

@@ -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<BackendBuilder['createHttpServer']>;
let backend: Backend;
let server: ReturnType<Backend['createHttpServer']>;
beforeEach(() => {
const app = application({
name: 'piano-service',


Loading…
Cancel
Save