From 218e754b2d1b0edca07495e8fc42c185e2f50b14 Mon Sep 17 00:00:00 2001 From: aminnairi <18418459+aminnairi@users.noreply.github.com> Date: Thu, 7 Dec 2023 20:41:30 +0100 Subject: [PATCH 1/3] renamed pathway to implementation --- src/jorel/createClientRoutes.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/jorel/createClientRoutes.ts b/src/jorel/createClientRoutes.ts index cd4eb1f..331499e 100644 --- a/src/jorel/createClientRoutes.ts +++ b/src/jorel/createClientRoutes.ts @@ -32,7 +32,7 @@ export class BadResponseError extends Error { /** * Define what arguments are necessary to send a client request to get a server response */ -export interface PathwayOptions { +export interface ClientImplementationOptions { /** * The parameters defined in the routes for this particular route */ @@ -46,13 +46,13 @@ export interface PathwayOptions { /** * Implementation of the callback that will be used to respond to clients requesting for this particular route */ -export type Pathway = (options: PathwayOptions) => Promise>; +export type ClientImplementation = (options: ClientImplementationOptions) => Promise>; /** * The whole implementations of the callbacks that will be used to respond to clients requesting for this particular route */ -export type Pathways = { - [Key in keyof R]: Pathway +export type ClientImplementations = { + [GenericRouteKey in keyof GenericRoute]: ClientImplementation } /** @@ -74,7 +74,7 @@ export interface CreateClientRoutesOptions { * Create a client that will send request to the server and use the routes as * the source of truth for all things related to request input */ -export const createClientRoutes = ({ server, routes }: CreateClientRoutesOptions): Pathways => { +export const createClientRoutes = ({ server, routes }: CreateClientRoutesOptions): ClientImplementations => { const routeWithCallbacks = Object.fromEntries(Object.entries(routes).map(([routeName, route]) => { const callback = async ({parameters, options}: { parameters: unknown, options: RequestInit}) => { const protectBody = Kalel.createProtector(route.request); @@ -122,5 +122,5 @@ export const createClientRoutes = ({ server, routes }: CreateC return [routeName, callback]; })); - return routeWithCallbacks as Pathways; + return routeWithCallbacks as ClientImplementations; }; From 3177f75fb3f0cd8eb2039cfbeaa2846f9927c04e Mon Sep 17 00:00:00 2001 From: aminnairi <18418459+aminnairi@users.noreply.github.com> Date: Thu, 7 Dec 2023 20:42:15 +0100 Subject: [PATCH 2/3] renamed implementation to server implementation --- src/jorel/createServerRoute.ts | 2 +- src/jorel/createServerRouter.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/jorel/createServerRoute.ts b/src/jorel/createServerRoute.ts index 93bd66b..2ef4e58 100644 --- a/src/jorel/createServerRoute.ts +++ b/src/jorel/createServerRoute.ts @@ -15,7 +15,7 @@ export interface CreateServerRouteOptions + response: Jorel.ServerImplementation } /** diff --git a/src/jorel/createServerRouter.ts b/src/jorel/createServerRouter.ts index 9ef233d..e079f1b 100644 --- a/src/jorel/createServerRouter.ts +++ b/src/jorel/createServerRouter.ts @@ -4,13 +4,13 @@ import { Route, Routes } from "./createRoutes"; /** * Implementation of a route, essentially just an asynchronous function that must respect the schema when returning a value */ -export type Implementation = (parameters: Kalel.InferType) => Promise> +export type ServerImplementation = (parameters: Kalel.InferType) => Promise> /** * The list of all implementations for a route */ -export type Implementations = { - [Key in keyof R]: Implementation +export type ServerImplementations = { + [GenericRouteKey in keyof GenericRoutes]: ServerImplementation } /** @@ -24,7 +24,7 @@ export interface CreateServerRouterOptions { /** * The concrete implementations of the routes's schema */ - implementations: Implementations + implementations: ServerImplementations } /** From 65a6e28319d47968fba6214de74ca698c9cd3ef5 Mon Sep 17 00:00:00 2001 From: aminnairi <18418459+aminnairi@users.noreply.github.com> Date: Thu, 7 Dec 2023 20:42:29 +0100 Subject: [PATCH 3/3] renamed one-letter generic types to be more readable --- src/jorel/createClientRoutes.ts | 6 +- src/jorel/createRoutes.ts | 2 +- src/jorel/createServerRouter.ts | 6 +- src/kalel.ts | 108 ++++++++++++++++---------------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/src/jorel/createClientRoutes.ts b/src/jorel/createClientRoutes.ts index 331499e..126788c 100644 --- a/src/jorel/createClientRoutes.ts +++ b/src/jorel/createClientRoutes.ts @@ -36,7 +36,7 @@ export interface ClientImplementationOptions { /** * The parameters defined in the routes for this particular route */ - parameters: Kalel.InferType, + parameters: Kalel.InferType, /** * The options used by the Web API Fetch that you can augment here, note that the headers will always contain the "Content-Type" and the "Accept" headers and cannot be overriden for practicality purposes */ @@ -58,7 +58,7 @@ export type ClientImplementations = { /** * Options used for create a new client to send requests to the server */ -export interface CreateClientRoutesOptions { +export interface CreateClientRoutesOptions { /** * The url to the server exposing the endpoints */ @@ -67,7 +67,7 @@ export interface CreateClientRoutesOptions { * The implementation of all available routes that have been defined in the * createRoutes function call */ - routes: R + routes: GenericRoutes } /** diff --git a/src/jorel/createRoutes.ts b/src/jorel/createRoutes.ts index ae51ccd..fbce4b1 100644 --- a/src/jorel/createRoutes.ts +++ b/src/jorel/createRoutes.ts @@ -25,6 +25,6 @@ export type Routes = { * Create a set of routes to be implemented later by the server, and consumed by * the client */ -export const createRoutes = (routes: R): R => { +export const createRoutes = (routes: GenericRoutes): GenericRoutes => { return routes; }; diff --git a/src/jorel/createServerRouter.ts b/src/jorel/createServerRouter.ts index e079f1b..b2cd6da 100644 --- a/src/jorel/createServerRouter.ts +++ b/src/jorel/createServerRouter.ts @@ -16,11 +16,11 @@ export type ServerImplementations = { /** * Options for creating a router */ -export interface CreateServerRouterOptions { +export interface CreateServerRouterOptions { /** * Route that have been created using the createRoute function */ - routes: R, + routes: GenericRoutes, /** * The concrete implementations of the routes's schema */ @@ -75,7 +75,7 @@ export type Router = (request: AdapterRequest) => Promise; /** * Create a router that can later be used with the http built-in module or express for instance */ -export const createServerRouter = ({ routes, implementations }: CreateServerRouterOptions) => { +export const createServerRouter = ({ routes, implementations }: CreateServerRouterOptions) => { return async (request: AdapterRequest) => { try { if (request.method === "OPTIONS") { diff --git a/src/kalel.ts b/src/kalel.ts index 45e95c6..b002f65 100644 --- a/src/kalel.ts +++ b/src/kalel.ts @@ -1,7 +1,7 @@ /** * Whenever a validation is a success */ -export interface ValidationSuccess { +export interface ValidationSuccess { /** * The state of this validation used for type discriminiation, in case of a success, this is always true */ @@ -9,7 +9,7 @@ export interface ValidationSuccess { /** * The data associated with the validation */ - data: InferType + data: InferType } /** @@ -43,7 +43,7 @@ export interface ValidationErrors { /** * Whether the validation is a success or a failure, you have to use type discrimination in order to get the correct type */ -export type Validation = ValidationSuccess | ValidationErrors +export type Validation = ValidationSuccess | ValidationErrors /** * The type of the rule that is applied for a given type @@ -138,7 +138,7 @@ export interface NumberSchema { /** * The schema definition used for validating arrays */ -export interface ArraySchema { +export interface ArraySchema { /** * The type of the schema that is used for validating arrays */ @@ -150,7 +150,7 @@ export interface ArraySchema { /** * The schema used for the type of the elements inside the array being validated */ - schema: S, + schema: GenericSchema, /** * A list of rules to apply to the array being validated */ @@ -160,7 +160,7 @@ export interface ArraySchema { /** * The fields inside the object being validated, each with a property name and a schema */ -export type ObjectSchemaFields = Record +export type ObjectSchemaFields = Record /** * The schema definition used for validating objects @@ -295,7 +295,7 @@ export interface LiteralSchema { /** * The type of the schema that is used for validating union values */ -export interface OneOfSchema { +export interface OneOfSchema { /** * The type of the schema that is used for validating union values */ @@ -303,7 +303,7 @@ export interface OneOfSchema { /** * The schema used for the type of the elements in the union being validated */ - schema: Array + schema: Array } /** @@ -339,56 +339,56 @@ export type Schema = /** * A utility type that can infer the type of a value based on its schema for basic schemas */ -export type InferBasicType = - S extends AnySchema +export type InferBasicType = + GenericBasicSchema extends AnySchema // eslint-disable-next-line @typescript-eslint/no-explicit-any ? any - : S extends UnknownSchema + : GenericBasicSchema extends UnknownSchema ? unknown - : S extends NumberSchema + : GenericBasicSchema extends NumberSchema ? number - : S extends StringSchema + : GenericBasicSchema extends StringSchema ? string - : S extends BooleanSchema + : GenericBasicSchema extends BooleanSchema ? boolean - : S extends DateSchema + : GenericBasicSchema extends DateSchema ? Date - : S extends NoneSchema + : GenericBasicSchema extends NoneSchema ? null - : S extends NotDefinedSchema + : GenericBasicSchema extends NotDefinedSchema ? undefined - : S extends EmptySchema + : GenericBasicSchema extends EmptySchema ? void - : S extends LiteralSchema + : GenericBasicSchema extends LiteralSchema ? InferedType - : S extends ArraySchema + : GenericBasicSchema extends ArraySchema ? Array> - : S extends ObjectSchema + : GenericBasicSchema extends ObjectSchema ? { [FieldKey in keyof Fields]: InferType } : never; /** * A utility type that can infer the type of a value based on its schema for constraints schemas */ -export type InferConstraintType = - S extends OneOfSchema +export type InferConstraintType = + GenericConstraintSchema extends OneOfSchema ? InferBasicType : never /** * A utility type that can infer the type of a value based on its schema */ -export type InferType = - S extends BasicSchema - ? InferBasicType - : S extends ConstraintSchema - ? InferConstraintType +export type InferType = + GenericSchema extends BasicSchema + ? InferBasicType + : GenericSchema extends ConstraintSchema + ? InferConstraintType : never /** * A function that can accept any values, and that returns a valiation, either a success or a failure and you have to use type discrimination in order to get the correct infered type out of this validation result */ -export type Validator = (data: unknown) => Validation +export type Validator = (data: unknown) => Validation /** * Options for the string schema function @@ -443,11 +443,11 @@ export const number = ({ message, rules }: NumberOptions): NumberSchema => { /** * Options for the array schema function */ -export interface ArrayOptions { +export interface ArrayOptions { /** * The schema to use for each item in the array */ - schema: S, + schema: GenericSchema, /** * The message to attach to the error */ @@ -461,7 +461,7 @@ export interface ArrayOptions { /** * Create a schema to validate arrays */ -export const array = ({ schema, message, rules }: ArrayOptions): ArraySchema => { +export const array = ({ schema, message, rules }: ArrayOptions): ArraySchema => { return { type: "array", schema, @@ -473,11 +473,11 @@ export const array = ({ schema, message, rules }: ArrayOptions /** * Options for the object schema function */ -export interface ObjectOptions> { +export interface ObjectOptions> { /** * The fields along with their schema */ - fields: F, + fields: GenericObjectSchemaFields, /** * The message to attach to the error */ @@ -487,7 +487,7 @@ export interface ObjectOptions /** * Create a schema to validate object */ -export const object = >({ fields, message }: ObjectOptions): ObjectSchema => { +export const object = >({ fields, message }: ObjectOptions): ObjectSchema => { return { type: "object", fields, @@ -646,7 +646,7 @@ export const literal = ({ message, value }: LiteralOptions): Liter /** * Create a schema to validate a union of values */ -export const oneOf = (schema: Array): OneOfSchema => { +export const oneOf = (schema: Array): OneOfSchema => { return { type: "oneOf", schema: schema @@ -658,7 +658,7 @@ export const oneOf = (schema: Array): OneOfSchema => { * @param schema The schema to apply for validation * @param initialPath The initial path (used internally for recursivity) */ -export const createProtector = (schema: S, initialPath: string = ""): Validator => { +export const createProtector = (schema: GenericSchema, initialPath: string = ""): Validator => { return data => { if (schema.type === "oneOf") { const validations = schema.schema.map(validation => { @@ -668,14 +668,14 @@ export const createProtector = (schema: S, initialPath: string return protection; }); - const isValidationSuccess = (validation: Validation): validation is ValidationSuccess => { + const isValidationSuccess = (validation: Validation): validation is ValidationSuccess => { return validation.success; }; const validationSuccesses = validations.filter(isValidationSuccess); if (validationSuccesses.length !== 0) { - const validationSuccess = validationSuccesses[0] as ValidationSuccess; + const validationSuccess = validationSuccesses[0] as ValidationSuccess; return { success: true, @@ -683,7 +683,7 @@ export const createProtector = (schema: S, initialPath: string }; } - const isValidationFailure = (validation: Validation): validation is ValidationErrors => { + const isValidationFailure = (validation: Validation): validation is ValidationErrors => { return !validation.success; }; @@ -712,7 +712,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } @@ -731,7 +731,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } @@ -750,14 +750,14 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } if (schema.type === "unknown") { return { success: true, - data: data as InferType + data: data as InferType }; } @@ -776,7 +776,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } @@ -795,14 +795,14 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } if (schema.type === "any") { return { success: true, - data: data as InferType + data: data as InferType }; } @@ -846,7 +846,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: date as InferType + data: date as InferType }; } @@ -888,7 +888,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } @@ -919,7 +919,7 @@ export const createProtector = (schema: S, initialPath: string return { success: true, - data: data as InferType + data: data as InferType }; } @@ -995,7 +995,7 @@ export const createProtector = (schema: S, initialPath: string return itemValidation.data; }).filter(itemValidation => { return itemValidation !== null; - }) as InferType; + }) as InferType; if (itemValidationErrors.length !== 0) { return { @@ -1032,7 +1032,7 @@ export const createProtector = (schema: S, initialPath: string fieldName, fieldValidation ]; - }) as Array<[string, Validation]>; + }) as Array<[string, Validation]>; const validationErrors = validations.flatMap(([, validation]) => { if (validation.success) { @@ -1055,9 +1055,9 @@ export const createProtector = (schema: S, initialPath: string ]; }).filter(validationEntry => { return validationEntry !== null; - }) as Array<[string, InferType]>; + }) as Array<[string, InferType]>; - const validationData = Object.fromEntries(validationEntries) as InferType; + const validationData = Object.fromEntries(validationEntries) as InferType; if (validationErrors.length !== 0) { return {