Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"useIgnoreFile": true
},
"formatter": {
"enabled": false,
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 120
"lineWidth": 100
},
"linter": {
"enabled": false,
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/app/__tests__/app-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ function createTestModule(name: string, prefix: string, routes: TestRoute[]) {
const moduleDef = createModuleDef({ name });
const router = moduleDef.router({ prefix });
for (const route of routes) {
const method = route.method.toLowerCase() as 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head';
const method = route.method.toLowerCase() as
| 'get'
| 'post'
| 'put'
| 'patch'
| 'delete'
| 'head';
router[method](route.path, { handler: route.handler });
}
return createModule(moduleDef, { services: [], routers: [router], exports: [] });
Expand Down Expand Up @@ -199,7 +205,9 @@ describe('createApp', () => {
]);

const app = createApp({}).register(mod);
const res = await app.handler(new Request('http://localhost/users/42/activate', { method: 'POST' }));
const res = await app.handler(
new Request('http://localhost/users/42/activate', { method: 'POST' }),
);

expect(res.status).toBe(204);
});
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/app/app-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ export function buildHandler(
});

const result = await entry.handler(ctx);
const response = result === undefined
? new Response(null, { status: 204 })
: createJsonResponse(result);
const response =
result === undefined ? new Response(null, { status: 204 }) : createJsonResponse(result);

if (config.cors) {
return applyCorsHeaders(config.cors, request, response);
Expand Down
9 changes: 2 additions & 7 deletions packages/core/src/di/boot-executor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
BootSequence,
ServiceBootInstruction,
} from '../types/boot-sequence';
import type { BootSequence, ServiceBootInstruction } from '../types/boot-sequence';
import { makeImmutable } from '../immutability';

interface ServiceEntry {
Expand Down Expand Up @@ -37,9 +34,7 @@ export class BootExecutor {

this.services.set(instr.id, {
methods: instr.factory.methods(deps, state),
onDestroy: instr.factory.onDestroy
? () => instr.factory.onDestroy!(deps, state)
: undefined,
onDestroy: instr.factory.onDestroy ? () => instr.factory.onDestroy!(deps, state) : undefined,
});
}

Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/env/__tests__/env-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ describe('createEnv', () => {

const env = createEnv({
schema: s.object({
DB: s.object({
HOST: s.string(),
PORT: s.string(),
}).default({ HOST: 'localhost', PORT: '5432' }),
DB: s
.object({
HOST: s.string(),
PORT: s.string(),
})
.default({ HOST: 'localhost', PORT: '5432' }),
}),
});

Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/module/__tests__/router-def.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ describe('moduleDef.router', () => {

expect(router.routes).toHaveLength(6);
expect(router.routes.map((r) => r.method)).toEqual([
'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD',
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'HEAD',
]);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/module/module-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export interface NamedModuleDef<
TImports extends Record<string, unknown> = Record<string, unknown>,
TOptions extends Record<string, unknown> = Record<string, unknown>,
> extends ModuleDef<TImports, TOptions> {
service: <TDeps, TState, TMethods>(config: ServiceDef<TDeps, TState, TMethods>) => NamedServiceDef<TDeps, TState, TMethods>;
service: <TDeps, TState, TMethods>(
config: ServiceDef<TDeps, TState, TMethods>,
) => NamedServiceDef<TDeps, TState, TMethods>;
router: (config: RouterDef) => NamedRouterDef;
}

Expand Down
16 changes: 6 additions & 10 deletions packages/core/src/module/service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import type { ServiceDef } from '../types/module';
import { deepFreeze } from '../immutability';

export interface NamedServiceDef<
TDeps = any,
TState = any,
TMethods = any,
> extends ServiceDef<TDeps, TState, TMethods> {
export interface NamedServiceDef<TDeps = any, TState = any, TMethods = any>
extends ServiceDef<TDeps, TState, TMethods> {
moduleName: string;
}

export function createServiceDef<
TDeps = any,
TState = any,
TMethods = any,
>(moduleName: string, config: ServiceDef<TDeps, TState, TMethods>): NamedServiceDef<TDeps, TState, TMethods> {
export function createServiceDef<TDeps = any, TState = any, TMethods = any>(
moduleName: string,
config: ServiceDef<TDeps, TState, TMethods>,
): NamedServiceDef<TDeps, TState, TMethods> {
return deepFreeze({
...config,
moduleName,
Expand Down
11 changes: 4 additions & 7 deletions packages/core/src/router/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,10 @@ export class Trie {
}

if (node.paramChild) {
const result = this.matchNode(
node.paramChild.node,
segments,
index + 1,
method,
{ ...params, [node.paramChild.name]: segment },
);
const result = this.matchNode(node.paramChild.node, segments, index + 1, method, {
...params,
[node.paramChild.name]: segment,
});
if (result) return result;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/server/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export function handleCors(config: CorsConfig, request: Request): Response | nul
return null;
}

export function applyCorsHeaders(config: CorsConfig, request: Request, response: Response): Response {
export function applyCorsHeaders(
config: CorsConfig,
request: Request,
response: Response,
): Response {
const requestOrigin = request.headers.get('origin');
const origin = resolveOrigin(config, requestOrigin);

Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/server/response-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { VertzException } from '../exceptions';

export function createJsonResponse(data: unknown, status = 200, headers?: Record<string, string>): Response {
export function createJsonResponse(
data: unknown,
status = 200,
headers?: Record<string, string>,
): Response {
return new Response(JSON.stringify(data), {
status,
headers: {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/types/boot-sequence.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { ServiceDef } from './module';

export type ServiceFactory<TDeps = any, TState = any, TMethods = any> = ServiceDef<TDeps, TState, TMethods>;
export type ServiceFactory<TDeps = any, TState = any, TMethods = any> = ServiceDef<
TDeps,
TState,
TMethods
>;

export interface ServiceBootInstruction {
type: 'service';
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/types/http.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';

export type HttpStatusCode =
| 200 | 201 | 204
| 301 | 302 | 304
| 400 | 401 | 403 | 404 | 405 | 409 | 422 | 429
| 500 | 502 | 503 | 504;
| 200
| 201
| 204
| 301
| 302
| 304
| 400
| 401
| 403
| 404
| 405
| 409
| 422
| 429
| 500
| 502
| 503
| 504;
10 changes: 2 additions & 8 deletions packages/core/src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ export interface ModuleDef<
options?: Schema<TOptions>;
}

export interface ServiceDef<
TDeps = any,
TState = any,
TMethods = any,
> {
export interface ServiceDef<TDeps = any, TState = any, TMethods = any> {
inject?: Record<string, unknown>;
onInit?: (deps: TDeps) => Promise<TState> | TState;
methods: (deps: TDeps, state: TState) => TMethods;
Expand All @@ -25,9 +21,7 @@ export interface RouterDef {
inject?: Record<string, unknown>;
}

export interface Module<
TDef extends ModuleDef = ModuleDef,
> {
export interface Module<TDef extends ModuleDef = ModuleDef> {
definition: TDef;
services: ServiceDef[];
routers: RouterDef[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ describe('Integration: Complex Compositions', () => {
expect(nameOnly.parse({ name: 'John' })).toEqual({ name: 'John' });

const extended = nameOnly.extend({ role: s.string() });
expect(extended.parse({ name: 'John', role: 'admin' })).toEqual({ name: 'John', role: 'admin' });
expect(extended.parse({ name: 'John', role: 'admin' })).toEqual({
name: 'John',
role: 'admin',
});

const partial = extended.partial();
expect(partial.parse({})).toEqual({});
Expand Down Expand Up @@ -44,7 +47,8 @@ describe('Integration: Complex Compositions', () => {
});

it('transform pipeline: string → parse → number → validate', () => {
const stringToNumber = s.string()
const stringToNumber = s
.string()
.transform((v) => parseInt(v, 10))
.pipe(s.number().int().gte(0));

Expand Down
20 changes: 12 additions & 8 deletions packages/schema/src/__tests__/integration/named-schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ describe('Integration: Named Schemas', () => {
});

it('named object with named nested schemas', () => {
const addressSchema = s.object({
street: s.string(),
city: s.string(),
}).id('Address');
const addressSchema = s
.object({
street: s.string(),
city: s.string(),
})
.id('Address');

const userSchema = s.object({
name: s.string(),
address: addressSchema,
}).id('UserWithAddress');
const userSchema = s
.object({
name: s.string(),
address: addressSchema,
})
.id('UserWithAddress');

const jsonSchema = userSchema.toJSONSchema();
expect(jsonSchema.$defs!['UserWithAddress']).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import type { Infer } from '../..';

describe('Integration: Recursive Schemas', () => {
it('tree node with s.lazy() and .id()', () => {
const treeSchema = s.object({
value: s.string(),
children: s.lazy(() => treeSchema).nullable(),
}).id('TreeNodeIntegration');
const treeSchema = s
.object({
value: s.string(),
children: s.lazy(() => treeSchema).nullable(),
})
.id('TreeNodeIntegration');

type TreeNode = Infer<typeof treeSchema>;

Expand Down Expand Up @@ -48,10 +50,12 @@ describe('Integration: Recursive Schemas', () => {
});

it('JSON Schema output with $ref for recursive schema', () => {
const nodeSchema = s.object({
name: s.string(),
children: s.array(s.lazy(() => nodeSchema)),
}).id('RecursiveNode');
const nodeSchema = s
.object({
name: s.string(),
children: s.array(s.lazy(() => nodeSchema)),
})
.id('RecursiveNode');

const jsonSchema = nodeSchema.toJSONSchema();
expect(jsonSchema.$ref).toBe('#/$defs/RecursiveNode');
Expand Down
18 changes: 8 additions & 10 deletions packages/schema/src/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ export class DefaultSchema<O, I> extends Schema<O, I | undefined> {
}

private _resolveDefault(): I {
return typeof this._default === 'function'
? (this._default as () => I)()
: this._default;
return typeof this._default === 'function' ? (this._default as () => I)() : this._default;
}

_clone(): DefaultSchema<O, I> {
Expand Down Expand Up @@ -311,10 +309,12 @@ export class RefinedSchema<O, I = O> extends Schema<O, I> {
}

_clone(): RefinedSchema<O, I> {
return this._cloneBase(new RefinedSchema(this._inner, this._predicate, {
message: this._message,
path: this._path,
}));
return this._cloneBase(
new RefinedSchema(this._inner, this._predicate, {
message: this._message,
path: this._path,
}),
);
}
}

Expand Down Expand Up @@ -442,9 +442,7 @@ export class CatchSchema<O, I = O> extends Schema<O, I> {
}

private _resolveFallback(): O {
return typeof this._fallback === 'function'
? (this._fallback as () => O)()
: this._fallback;
return typeof this._fallback === 'function' ? (this._fallback as () => O)() : this._fallback;
}

_clone(): CatchSchema<O, I> {
Expand Down
4 changes: 1 addition & 3 deletions packages/schema/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,4 @@ export interface SchemaMetadata {
examples: unknown[];
}

export type SafeParseResult<T> =
| { success: true; data: T }
| { success: false; error: ParseError };
export type SafeParseResult<T> = { success: true; data: T } | { success: false; error: ParseError };
8 changes: 6 additions & 2 deletions packages/schema/src/effects/__tests__/readonly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe('.readonly()', () => {
name: new StringSchema(),
}).readonly();
const result = schema.parse({ name: 'Alice' });
expect(() => { (result as any).name = 'Bob'; }).toThrow();
expect(() => {
(result as any).name = 'Bob';
}).toThrow();
});

it('infers Readonly<T> type', () => {
Expand All @@ -36,7 +38,9 @@ describe('.readonly()', () => {
const schema = new ArraySchema(new StringSchema()).readonly();
const result = schema.parse(['a', 'b']);
expect(Object.isFrozen(result)).toBe(true);
expect(() => { (result as any).push('c'); }).toThrow();
expect(() => {
(result as any).push('c');
}).toThrow();
});

it('passes primitives through unchanged', () => {
Expand Down
Loading