Skip to content

Commit

Permalink
replaced occurrences of spaceships with implementations in the
Browse files Browse the repository at this point in the history
documentation
  • Loading branch information
aminnairi committed Dec 7, 2023
1 parent c63cdbe commit abd7854
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ import { routes } from "@template/shared";
const router = Kryptonian.Jorel.createServerRouter({
clients: ["http://localhost:5173"],
routes,
spaceships: {
implementations: {
getKryptonians: async () => {
return [
{
Expand All @@ -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");
});
```

Expand Down Expand Up @@ -2491,7 +2491,7 @@ import { createHttpServer } from "./adapters/createHttpServer";

const router = Kryptonian.Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getKryptonians,
createKryptonian
}
Expand All @@ -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");
});
```

Expand Down Expand Up @@ -2668,15 +2668,15 @@ const router = Kryptonian.Jorel.createServerRouter({
clients: [
"http://localhost:5173"
],
spaceships: {
implementations: {
getUsers
}
});

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!");
});
```

Expand Down Expand Up @@ -3036,4 +3036,4 @@ See [`LICENSE`](./LICENSE).

See [`SECURITY.md`](./SECURITY.md).

[Back to summary](#summary)
[Back to summary](#summary)
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.Spaceship<GenericRoutes[GenericRouteName]>
response: Jorel.Implementation<GenericRoutes[GenericRouteName]>
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/jorel/createServerRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Vitest.describe("createRouter", () => {

const router = createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return null;
}
Expand All @@ -42,7 +42,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return null;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return null;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return null;
}
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -196,7 +196,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return null;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -280,7 +280,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
return [
"first",
Expand Down Expand Up @@ -323,7 +323,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
throw new Error("Oops!");
}
Expand Down Expand Up @@ -370,7 +370,7 @@ Vitest.describe("createRouter", () => {

const router = Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getUsers: async () => {
throw "Uncommon";
}
Expand All @@ -396,4 +396,4 @@ Vitest.describe("createRouter", () => {
]
});
});
});
});
28 changes: 14 additions & 14 deletions src/jorel/createServerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<R extends Route> = (parameters: Kalel.InferType<R["request"]>) => Promise<Kalel.InferType<R["response"]>>
export type Implementation<R extends Route> = (parameters: Kalel.InferType<R["request"]>) => Promise<Kalel.InferType<R["response"]>>

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

/**
Expand All @@ -24,7 +24,7 @@ export interface CreateServerRouterOptions<R extends Routes> {
/**
* The concrete implementations of the routes's schema
*/
spaceships: Spaceships<R>
implementations: Implementations<R>
}

/**
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, spaceships }: CreateServerRouterOptions<R>) => {
export const createServerRouter = <R extends Routes>({ routes, implementations }: CreateServerRouterOptions<R>) => {
return async (request: AdapterRequest) => {
try {
if (request.method === "OPTIONS") {
Expand Down Expand Up @@ -125,9 +125,9 @@ export const createServerRouter = <R extends Routes>({ routes, spaceships }: Cre
}

const [routeName, route] = foundRoute;
const spaceship = spaceships[routeName];
const implementation = implementations[routeName];

if (!spaceship) {
if (!implementation) {
return {
status: 412,
headers: {},
Expand All @@ -154,23 +154,23 @@ export const createServerRouter = <R extends Routes>({ 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 {
Expand All @@ -188,4 +188,4 @@ export const createServerRouter = <R extends Routes>({ routes, spaceships }: Cre
};
}
};
};
};
6 changes: 3 additions & 3 deletions template/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createExpressServer } from "./adapters/createExpressServer";

const router = Kryptonian.Jorel.createServerRouter({
routes,
spaceships: {
implementations: {
getKryptonians
}
});
Expand All @@ -16,5 +16,5 @@ const server = createExpressServer({
});

server.listen(8000, "0.0.0.0", () => {
console.log("Spaceship launched and ready for communications");
});
console.log("Server launched and ready for communications");
});

0 comments on commit abd7854

Please sign in to comment.