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

Renamed pathway occurrences to implementation #135

Merged
merged 3 commits into from
Dec 7, 2023
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
18 changes: 9 additions & 9 deletions src/jorel/createClientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export class BadResponseError extends Error {
/**
* Define what arguments are necessary to send a client request to get a server response
*/
export interface PathwayOptions<R extends Route> {
export interface ClientImplementationOptions<GenericRoute extends Route> {
/**
* The parameters defined in the routes for this particular route
*/
parameters: Kalel.InferType<R["request"]>,
parameters: Kalel.InferType<GenericRoute["request"]>,
/**
* 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
*/
Expand All @@ -46,19 +46,19 @@ export interface PathwayOptions<R extends Route> {
/**
* Implementation of the callback that will be used to respond to clients requesting for this particular route
*/
export type Pathway<R extends Route> = (options: PathwayOptions<R>) => Promise<Kalel.InferType<R["response"]>>;
export type ClientImplementation<GenericRoute extends Route> = (options: ClientImplementationOptions<GenericRoute>) => Promise<Kalel.InferType<GenericRoute["response"]>>;

/**
* The whole implementations of the callbacks that will be used to respond to clients requesting for this particular route
*/
export type Pathways<R extends Routes> = {
[Key in keyof R]: Pathway<R[Key]>
export type ClientImplementations<GenericRoute extends Routes> = {
[GenericRouteKey in keyof GenericRoute]: ClientImplementation<GenericRoute[GenericRouteKey]>
}

/**
* Options used for create a new client to send requests to the server
*/
export interface CreateClientRoutesOptions<R extends Routes> {
export interface CreateClientRoutesOptions<GenericRoutes extends Routes> {
/**
* The url to the server exposing the endpoints
*/
Expand All @@ -67,14 +67,14 @@ export interface CreateClientRoutesOptions<R extends Routes> {
* The implementation of all available routes that have been defined in the
* createRoutes function call
*/
routes: R
routes: GenericRoutes
}

/**
* 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 = <R extends Routes>({ server, routes }: CreateClientRoutesOptions<R>): Pathways<R> => {
export const createClientRoutes = <GenericRoutes extends Routes>({ server, routes }: CreateClientRoutesOptions<GenericRoutes>): ClientImplementations<GenericRoutes> => {
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);
Expand Down Expand Up @@ -122,5 +122,5 @@ export const createClientRoutes = <R extends Routes>({ server, routes }: CreateC
return [routeName, callback];
}));

return routeWithCallbacks as Pathways<R>;
return routeWithCallbacks as ClientImplementations<GenericRoutes>;
};
2 changes: 1 addition & 1 deletion src/jorel/createRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <R extends Routes>(routes: R): R => {
export const createRoutes = <GenericRoutes extends Routes>(routes: GenericRoutes): GenericRoutes => {
return routes;
};
2 changes: 1 addition & 1 deletion src/jorel/createServerRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface CreateServerRouteOptions<GenericRoutes extends Jorel.Routes, Ge
/**
* The implementation of the route's response that should match the defined route response schema, you'll also get access to the route parameters if defined
*/
response: Jorel.Implementation<GenericRoutes[GenericRouteName]>
response: Jorel.ServerImplementation<GenericRoutes[GenericRouteName]>
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/jorel/createServerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ 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<R extends Route> = (parameters: Kalel.InferType<R["request"]>) => Promise<Kalel.InferType<R["response"]>>
export type ServerImplementation<GenericRoute extends Route> = (parameters: Kalel.InferType<GenericRoute["request"]>) => Promise<Kalel.InferType<GenericRoute["response"]>>

/**
* The list of all implementations for a route
*/
export type Implementations<R extends Routes> = {
[Key in keyof R]: Implementation<R[Key]>
export type ServerImplementations<GenericRoutes extends Routes> = {
[GenericRouteKey in keyof GenericRoutes]: ServerImplementation<GenericRoutes[GenericRouteKey]>
}

/**
* Options for creating a router
*/
export interface CreateServerRouterOptions<R extends Routes> {
export interface CreateServerRouterOptions<GenericRoutes extends Routes> {
/**
* Route that have been created using the createRoute function
*/
routes: R,
routes: GenericRoutes,
/**
* The concrete implementations of the routes's schema
*/
implementations: Implementations<R>
implementations: ServerImplementations<GenericRoutes>
}

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ export type Router = (request: AdapterRequest) => Promise<RouterResponse>;
/**
* Create a router that can later be used with the http built-in module or express for instance
*/
export const createServerRouter = <R extends Routes>({ routes, implementations }: CreateServerRouterOptions<R>) => {
export const createServerRouter = <GenericRoutes extends Routes>({ routes, implementations }: CreateServerRouterOptions<GenericRoutes>) => {
return async (request: AdapterRequest) => {
try {
if (request.method === "OPTIONS") {
Expand Down
Loading