diff --git a/README.md b/README.md index 80ec0ac..87fc041 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ import { routes } from "@template/shared"; const router = Kryptonian.Jorel.createServerRouter({ clients: ["http://localhost:5173"], routes, - spaceships: { + implementations: { getKryptonians: async () => { return [ { @@ -204,7 +204,7 @@ const router = Kryptonian.Jorel.createServerRouter({ const server = Http.createServer(router); server.listen(8000, "0.0.0.0", () => { - console.log("Spaceship launched and ready for communications"); + console.log("Server launched and ready for communications"); }); ``` @@ -2491,7 +2491,7 @@ import { createHttpServer } from "./adapters/createHttpServer"; const router = Kryptonian.Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getKryptonians, createKryptonian } @@ -2503,7 +2503,7 @@ const server = createHttpServer({ }); server.listen(8000, "0.0.0.0", () => { - console.log("Spaceship launched and ready for communications"); + console.log("Server launched and ready for communications"); }); ``` @@ -2668,7 +2668,7 @@ const router = Kryptonian.Jorel.createServerRouter({ clients: [ "http://localhost:5173" ], - spaceships: { + implementations: { getUsers } }); @@ -2676,7 +2676,7 @@ const router = Kryptonian.Jorel.createServerRouter({ const server = Http.createServer(router); server.listen(8000, "0.0.0.0", () => { - console.log("Spaceships launched and ready for communications!"); + console.log("Server launched and ready for communications!"); }); ``` @@ -3036,4 +3036,4 @@ See [`LICENSE`](./LICENSE). See [`SECURITY.md`](./SECURITY.md). -[Back to summary](#summary) \ No newline at end of file +[Back to summary](#summary) diff --git a/src/jorel/createServerRoute.ts b/src/jorel/createServerRoute.ts index f39eb08..93bd66b 100644 --- a/src/jorel/createServerRoute.ts +++ b/src/jorel/createServerRoute.ts @@ -15,7 +15,7 @@ export interface CreateServerRouteOptions + response: Jorel.Implementation } /** diff --git a/src/jorel/createServerRouter.test.ts b/src/jorel/createServerRouter.test.ts index 30c18c4..9bc0702 100644 --- a/src/jorel/createServerRouter.test.ts +++ b/src/jorel/createServerRouter.test.ts @@ -18,7 +18,7 @@ Vitest.describe("createRouter", () => { const router = createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return null; } @@ -42,7 +42,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return null; } @@ -75,7 +75,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return null; } @@ -116,7 +116,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return null; } @@ -158,7 +158,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, // @ts-expect-error This should never happen when using TypeScript - spaceships: {} + implementations: {} }); const response = await router({ @@ -196,7 +196,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return null; } @@ -235,7 +235,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { // @ts-expect-error This should never happen when using TypeScript getUsers: async () => { return 123; @@ -280,7 +280,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { return [ "first", @@ -323,7 +323,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { throw new Error("Oops!"); } @@ -370,7 +370,7 @@ Vitest.describe("createRouter", () => { const router = Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getUsers: async () => { throw "Uncommon"; } @@ -396,4 +396,4 @@ Vitest.describe("createRouter", () => { ] }); }); -}); \ No newline at end of file +}); diff --git a/src/jorel/createServerRouter.ts b/src/jorel/createServerRouter.ts index a390d6e..9ef233d 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 Spaceship = (parameters: Kalel.InferType) => Promise> +export type Implementation = (parameters: Kalel.InferType) => Promise> /** * The list of all implementations for a route */ -export type Spaceships = { - [Key in keyof R]: Spaceship +export type Implementations = { + [Key in keyof R]: Implementation } /** @@ -24,7 +24,7 @@ export interface CreateServerRouterOptions { /** * The concrete implementations of the routes's schema */ - spaceships: Spaceships + implementations: Implementations } /** @@ -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, spaceships }: CreateServerRouterOptions) => { +export const createServerRouter = ({ routes, implementations }: CreateServerRouterOptions) => { return async (request: AdapterRequest) => { try { if (request.method === "OPTIONS") { @@ -125,9 +125,9 @@ export const createServerRouter = ({ routes, spaceships }: Cre } const [routeName, route] = foundRoute; - const spaceship = spaceships[routeName]; + const implementation = implementations[routeName]; - if (!spaceship) { + if (!implementation) { return { status: 412, headers: {}, @@ -154,23 +154,23 @@ export const createServerRouter = ({ routes, spaceships }: Cre }; } - const spaceshipResponse = await spaceship(bodyProtection.data); + const response = await implementation(bodyProtection.data); - const protectSpaceshipResponse = Kalel.createProtector(route.response); - const spaceshipResponseProtection = protectSpaceshipResponse(spaceshipResponse); + const protectResponse = Kalel.createProtector(route.response); + const responseProtection = protectResponse(response); - if (!spaceshipResponseProtection.success) { + if (!responseProtection.success) { return { status: 400, headers: {}, - body: spaceshipResponseProtection.errors + body: responseProtection.errors }; } return { status: 200, headers: {}, - body: spaceshipResponseProtection.data + body: responseProtection.data }; } catch (error) { return { @@ -188,4 +188,4 @@ export const createServerRouter = ({ routes, spaceships }: Cre }; } }; -}; \ No newline at end of file +}; diff --git a/template/server/index.ts b/template/server/index.ts index 38aed1f..4827a1c 100644 --- a/template/server/index.ts +++ b/template/server/index.ts @@ -5,7 +5,7 @@ import { createExpressServer } from "./adapters/createExpressServer"; const router = Kryptonian.Jorel.createServerRouter({ routes, - spaceships: { + implementations: { getKryptonians } }); @@ -16,5 +16,5 @@ const server = createExpressServer({ }); server.listen(8000, "0.0.0.0", () => { - console.log("Spaceship launched and ready for communications"); -}); \ No newline at end of file + console.log("Server launched and ready for communications"); +});