Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved ts config, fixed types, insttead of PR 404 #407

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.words": ["fets"]
}
5 changes: 5 additions & 0 deletions benchmark/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ const router = createRouter({
handler,
});

// @ts-ignore Types of parameters 'req' and 'req' are incompatible.
// Type 'IncomingMessage' is not assignable to type 'NodeRequest' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
// Types of property 'url' are incompatible.
// Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
createServer(router).listen(4000, () => {
readyCount++;
console.log('listening on 0.0.0.0:4000');
Expand Down
4 changes: 2 additions & 2 deletions e2e/shared-scripts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { OutputValue, Stack } from '@pulumi/pulumi/automation';

export type DeploymentConfiguration<TProgramOutput = any> = {
name: string;
prerequisites?: (stack: Stack) => Promise<void>;
config?: (stack: Stack) => Promise<void>;
prerequisites?: ((stack: Stack) => Promise<void>) | undefined;
config?: ((stack: Stack) => Promise<void>) | undefined;
program: () => Promise<{
[K in keyof TProgramOutput]: Output<TProgramOutput[K]> | TProgramOutput[K];
}>;
Expand Down
2 changes: 1 addition & 1 deletion e2e/shared-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRouter, Response, useErrorHandling } from 'fets';
import { z } from 'zod';

export function createTestServerAdapter<TServerContext = {}>(base?: string) {
export function createTestServerAdapter<TServerContext = {}>(base?: string | undefined) {
return createRouter<TServerContext, {}>({
base,
plugins: [useErrorHandling()],
Expand Down
17 changes: 11 additions & 6 deletions examples/todolist/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const router = createRouter({
schemas: {
Todo: TodoSchema,
},
} as const,
},
},
})
.route({
Expand All @@ -42,7 +42,9 @@ export const router = createRouter({
},
},
} as const,
handler: () => Response.json(todos),
handler: () => {
return Response.json(todos);
},
})
.route({
description: 'Get a todo',
Expand Down Expand Up @@ -193,7 +195,7 @@ export const router = createRouter({
required: ['file'],
additionalProperties: false,
},
},
} as const,
responses: {
200: {
type: 'object',
Expand All @@ -213,12 +215,15 @@ export const router = createRouter({
const body = await request.formData();
const file = body.get('file');
const description = body.get('description');
return Response.json({

const json = {
name: file.name,
description,
description: description || '',
type: file.type,
size: file.size,
lastModified: file.lastModified,
});
} as const;

return Response.json(json);
},
});
6 changes: 6 additions & 0 deletions examples/zod-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export const router = createRouter()
},
});

// TODO: Type 'IncomingMessage' is not assignable to type 'NodeRequest' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
// Types of property 'url' are incompatible.
// Type 'string | undefined' is not assignable to type 'string'.
// Type 'undefined' is not assignable to type 'string'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
createServer(router).listen(3000, () => {
console.log('SwaggerUI is served at http://localhost:3000/docs');
});
1 change: 1 addition & 0 deletions examples/zod-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
Expand Down
28 changes: 23 additions & 5 deletions packages/fets/src/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const HTTP_METHODS: HTTPMethod[] = [
const EMPTY_OBJECT = {};
const EMPTY_MATCH = { pathname: { groups: {} } } as URLPatternResult;

export function createRouterBase(
export function createRouterBase<TServerContext>(
{
fetchAPI: givenFetchAPI,
base: basePath = '/',
plugins = [],
swaggerUI,
}: RouterOptions<any, any> = {},
}: RouterOptions<TServerContext, any>,
openAPIDocument: OpenAPIDocument,
): RouterBaseObject<any, any, any> {
const fetchAPI = {
Expand Down Expand Up @@ -325,19 +325,20 @@ export function createRouterBase(

export function createRouter<
TServerContext,
TComponents extends RouterComponentsBase = {},
TComponents extends RouterComponentsBase,
TRouterSDK extends RouterSDK<string, TypedRequest, TypedResponse> = {
[TKey: string]: never;
},
>(
options: RouterOptions<TServerContext, TComponents> = {},
options?: RouterOptions<TServerContext, TComponents> | undefined,
): Router<TServerContext, TComponents, TRouterSDK> {
const {
openAPI: { endpoint: oasEndpoint = '/openapi.json', ...openAPIDocument } = {},
swaggerUI: { endpoint: swaggerUIEndpoint = '/docs', ...swaggerUIOpts } = {},
plugins: userPlugins = [],
base = '/',
} = options;
} = options || {};

openAPIDocument.openapi = openAPIDocument.openapi || '3.0.1';
const oasInfo = (openAPIDocument.info ||= {} as OpenAPIInfo);
oasInfo.title ||= 'feTS API';
Expand Down Expand Up @@ -373,9 +374,26 @@ export function createRouter<
plugins,
};
const routerBaseObject = createRouterBase(finalOpts, openAPIDocument as OpenAPIDocument);

// Argument of type 'RouterOptions<TServerContext, TComponents>' is not assignable to parameter of type 'ServerAdapterOptions<TServerContext>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
// Types of property 'plugins' are incompatible.
// Type 'RouterPlugin<TServerContext>[] | undefined' is not assignable to type 'ServerAdapterPlugin<TServerContext>[]'.
// Type 'undefined' is not assignable to type 'ServerAdapterPlugin<TServerContext>[]'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const router = createServerAdapter(routerBaseObject, finalOpts);

for (const onRouterInitHook of routerBaseObject.__onRouterInitHooks) {
// Argument of type 'ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, ServerAdapterRequestHandler<TServerContext>>>' is not assignable to parameter of type 'Router<any, any, any>'.
// Type 'ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, ServerAdapterRequestHandler<TServerContext>>>' is not assignable to type 'RouterBaseObject<any, any, any>'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onRouterInitHook(router);
}

// Type 'ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, ServerAdapterRequestHandler<TServerContext>>>' is not assignable to type 'Router<TServerContext, TComponents, TRouterSDK>'.
// Type 'ServerAdapter<TServerContext, ServerAdapterBaseObject<TServerContext, ServerAdapterRequestHandler<TServerContext>>>' is not assignable to type 'RouterBaseObject<TServerContext, TComponents, TRouterSDK>'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return router;
}
40 changes: 33 additions & 7 deletions packages/fets/src/plugins/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import addFormats from 'ajv-formats';
import jsonSerializerFactory from '@ardatan/fast-json-stringify';
import { URL } from '@whatwg-node/fetch';
import { Response } from '../Response.js';
import { StatusCode } from '../typed-fetch.js';
import {
import type { StatusCode } from '../typed-fetch.js';
import type {
JSONSerializer,
PromiseOrValue,
RouterComponentsBase,
Expand All @@ -18,9 +18,9 @@ import { getHeadersObj } from './utils.js';
type ValidateRequestFn = (request: RouterRequest) => PromiseOrValue<ErrorObject[]>;

export function useAjv({
components = {},
components = { schemas: {} },
}: {
components?: RouterComponentsBase;
components?: RouterComponentsBase | undefined;
} = {}): RouterPlugin<any> {
const ajv = new Ajv({
strict: false,
Expand Down Expand Up @@ -73,10 +73,16 @@ export function useAjv({
onRoute({ path, schemas, handlers }) {
const validationMiddlewares = new Map<string, ValidateRequestFn>();
if (schemas?.request?.headers && !isZodSchema(schemas.request.headers)) {
const { headers } = schemas.request;

// TODO: Property '$async' is missing in type '{ components: RouterComponentsBase; type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; pattern?: string | undefined; ... 46 more ...; [$JSONSchema7]?: unique symbol; }' but required in type 'AsyncSchema'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const validateFn = ajv.compile({
...schemas.request.headers,
...headers,
components,
});

validationMiddlewares.set('headers', request => {
const headersObj = getHeadersObj(request.headers);
const isValid = validateFn(headersObj);
Expand All @@ -87,10 +93,14 @@ export function useAjv({
});
}
if (schemas?.request?.params && !isZodSchema(schemas.request.params)) {
// TODO: Property '$async' is missing in type '{ components: RouterComponentsBase; type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; pattern?: string | undefined; ... 46 more ...; [$JSONSchema7]?: unique symbol; }' but required in type 'AsyncSchema'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const validateFn = ajv.compile({
...schemas.request.params,
components,
});

validationMiddlewares.set('params', request => {
const isValid = validateFn(request.params);
if (!isValid) {
Expand All @@ -100,10 +110,14 @@ export function useAjv({
});
}
if (schemas?.request?.query && !isZodSchema(schemas.request.query)) {
// TODO: Property '$async' is missing in type '{ components: RouterComponentsBase; type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; pattern?: string | undefined; ... 46 more ...; [$JSONSchema7]?: unique symbol; }' but required in type 'AsyncSchema'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const validateFn = ajv.compile({
...schemas.request.query,
components,
});

validationMiddlewares.set('query', request => {
const isValid = validateFn(request.query);
if (!isValid) {
Expand All @@ -113,10 +127,14 @@ export function useAjv({
});
}
if (schemas?.request?.json && !isZodSchema(schemas.request.json)) {
// TODO: Property '$async' is missing in type '{ components: RouterComponentsBase; type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; pattern?: string | undefined; ... 46 more ...; [$JSONSchema7]?: unique symbol; }' but required in type 'AsyncSchema'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const validateFn = ajv.compile({
...schemas.request.json,
components,
});

validationMiddlewares.set('json', async request => {
const contentType = request.headers.get('content-type');
if (contentType?.includes('json')) {
Expand All @@ -134,25 +152,33 @@ export function useAjv({
});
}
if (schemas?.request?.formData && !isZodSchema(schemas.request.formData)) {
// TODO: Property '$async' is missing in type '{ components: RouterComponentsBase; type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined; pattern?: string | undefined; ... 46 more ...; [$JSONSchema7]?: unique symbol; }' but required in type 'AsyncSchema'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const validateFn = ajv.compile({
...schemas.request.formData,
components,
});

validationMiddlewares.set('formData', async request => {
const contentType = request.headers.get('content-type');
if (
contentType?.includes('multipart/form-data') ||
contentType?.includes('application/x-www-form-urlencoded')
) {
const formData = await request.formData();
const formDataObj: Record<string, FormDataEntryValue> = {};
const formDataObj: Record<string, FormDataEntryValue | undefined> = {};
const jobs: Promise<void>[] = [];
formData.forEach((value, key) => {
if (typeof value === 'undefined') {
return;
}

if (typeof value === 'string') {
formDataObj[key] = value;
} else {
jobs.push(
value.arrayBuffer().then(buffer => {
value.arrayBuffer().then((buffer: ArrayBuffer): void => {
const typedArray = new Uint8Array(buffer);
const binaryStrParts: string[] = [];
typedArray.forEach((byte, index) => {
Expand Down
Loading
Loading