@@ -3,7 +3,7 @@ import {createServer, CreateServerParams, Server} from './servers/http'; | |||||
import {BackendState} from './common'; | import {BackendState} from './common'; | ||||
import {DataSource} from './data-source'; | import {DataSource} from './data-source'; | ||||
export interface BackendBuilder<T extends DataSource = DataSource> { | |||||
export interface Backend<T extends DataSource = DataSource> { | |||||
showTotalItemCountOnGetCollection(b?: boolean): this; | showTotalItemCountOnGetCollection(b?: boolean): this; | ||||
showTotalItemCountOnCreateItem(b?: boolean): this; | showTotalItemCountOnCreateItem(b?: boolean): this; | ||||
checksSerializersOnDelete(b?: boolean): this; | checksSerializersOnDelete(b?: boolean): this; | ||||
@@ -52,5 +52,5 @@ export const createBackend = (params: CreateBackendParams) => { | |||||
createHttpServer(serverParams = {} as CreateServerParams) { | createHttpServer(serverParams = {} as CreateServerParams) { | ||||
return createServer(backendState, serverParams); | return createServer(backendState, serverParams); | ||||
}, | }, | ||||
} satisfies BackendBuilder; | |||||
} satisfies Backend; | |||||
}; | }; |
@@ -3,7 +3,7 @@ import {FALLBACK_LANGUAGE, Language} from './language'; | |||||
import {FALLBACK_MEDIA_TYPE, MediaType} from './media-type'; | import {FALLBACK_MEDIA_TYPE, MediaType} from './media-type'; | ||||
import {Charset, FALLBACK_CHARSET} from './charset'; | import {Charset, FALLBACK_CHARSET} from './charset'; | ||||
import * as v from 'valibot'; | import * as v from 'valibot'; | ||||
import {BackendBuilder, createBackend, CreateBackendParams} from '../backend'; | |||||
import {Backend, createBackend, CreateBackendParams} from '../backend'; | |||||
import {ClientBuilder, createClient, CreateClientParams} from '../client'; | import {ClientBuilder, createClient, CreateClientParams} from '../client'; | ||||
type ApplicationMap<T extends { name: string }> = Map<T['name'], T>; | type ApplicationMap<T extends { name: string }> = Map<T['name'], T>; | ||||
@@ -20,20 +20,33 @@ export interface ApplicationParams { | |||||
name: string; | 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< | resource< | ||||
Schema extends v.BaseSchema, | Schema extends v.BaseSchema, | ||||
CurrentItemName extends string = string, | CurrentItemName extends string = string, | ||||
CurrentRouteName 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; | createClient(params: Omit<CreateClientParams, 'app'>): ClientBuilder; | ||||
} | } | ||||
export const application = (appParams: ApplicationParams): ApplicationBuilder => { | |||||
export const application = (appParams: ApplicationParams): Application => { | |||||
const appState: ApplicationState = { | const appState: ApplicationState = { | ||||
name: appParams.name, | name: appParams.name, | ||||
resources: new Set<Resource<any>>(), | resources: new Set<Resource<any>>(), | ||||
@@ -1,5 +1,5 @@ | |||||
export interface Charset { | |||||
name: string; | |||||
export interface Charset<Name extends string = string> { | |||||
name: Name; | |||||
encode: (str: string) => Buffer; | encode: (str: string) => Buffer; | ||||
decode: (buf: Buffer) => string; | decode: (buf: Buffer) => string; | ||||
} | } | ||||
@@ -51,8 +51,8 @@ export type LanguageDefaultBodyKey = typeof LANGUAGE_DEFAULT_BODY_KEYS[number]; | |||||
export interface LanguageBodyMap extends Record<LanguageDefaultBodyKey, MessageBody> {} | export interface LanguageBodyMap extends Record<LanguageDefaultBodyKey, MessageBody> {} | ||||
export interface Language { | |||||
name: string, | |||||
export interface Language<Name extends string = string> { | |||||
name: Name, | |||||
statusMessages: LanguageStatusMessageMap, | statusMessages: LanguageStatusMessageMap, | ||||
bodies: LanguageBodyMap | bodies: LanguageBodyMap | ||||
} | } | ||||
@@ -1,5 +1,5 @@ | |||||
export interface MediaType { | |||||
name: string; | |||||
export interface MediaType<Name extends string = string> { | |||||
name: Name; | |||||
serialize: <T>(object: T) => string; | serialize: <T>(object: T) => string; | ||||
deserialize: <T>(s: string) => T; | deserialize: <T>(s: string) => T; | ||||
} | } | ||||
@@ -2,8 +2,8 @@ import {describe, afterAll, afterEach, beforeAll, beforeEach, it} from 'vitest'; | |||||
import {mkdtemp, rm} from 'fs/promises'; | import {mkdtemp, rm} from 'fs/promises'; | ||||
import {join} from 'path'; | import {join} from 'path'; | ||||
import {tmpdir} from 'os'; | 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 {autoIncrement} from '../fixtures'; | ||||
import {RequestContext} from '../../src/backend/common'; | import {RequestContext} from '../../src/backend/common'; | ||||
@@ -52,7 +52,7 @@ describe('decorators', () => { | |||||
}); | }); | ||||
}); | }); | ||||
let server: ReturnType<BackendBuilder['createHttpServer']>; | |||||
let server: ReturnType<Backend['createHttpServer']>; | |||||
beforeEach(() => { | beforeEach(() => { | ||||
const app = application({ | const app = application({ | ||||
name: 'piano-service', | name: 'piano-service', | ||||
@@ -20,7 +20,7 @@ import { | |||||
} from 'path'; | } from 'path'; | ||||
import {request} from 'http'; | import {request} from 'http'; | ||||
import {constants} from 'http2'; | 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 { application, resource, validation as v, Resource } from '../../src/common'; | ||||
import { autoIncrement } from '../fixtures'; | 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(() => { | beforeEach(() => { | ||||
const app = application({ | const app = application({ | ||||
name: 'piano-service', | name: 'piano-service', | ||||