Skip to content

Commit

Permalink
feat: api modelling base (#1)
Browse files Browse the repository at this point in the history
Base design for the intended REST api functions
  • Loading branch information
oshinongit authored Nov 8, 2023
1 parent 7ebe079 commit 76b1d24
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 9 deletions.
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
testEnvironment: 'node'
};
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"watch": ["src"],
"ext": "ts",
"exec": "node --inspect -r ts-node/register ./src/server.ts"
}
}
4 changes: 2 additions & 2 deletions readme-typescript-nodejs.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# typescript-nodejs
# typescript-nodejs

Requirements: node.js >= 18.15.0 ( LTS )

Expand All @@ -14,4 +14,4 @@ Template for TypeScript nodejs projects. It includes:

1. Update package.json with your project details
2. Install dependencies with `npm install`
3. Start coding!
3. Start coding!
8 changes: 6 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export interface HealthcheckOptions {
title: string;
}

const healthcheck: FastifyPluginCallback<HealthcheckOptions> = (fastify, opts, next) => {
const healthcheck: FastifyPluginCallback<HealthcheckOptions> = (
fastify,
opts,
next
) => {
fastify.get<{ Reply: Static<typeof HelloWorld> }>(
'/',
{
Expand Down Expand Up @@ -62,4 +66,4 @@ export default (opts: ApiOptions) => {
// register other API routes here

return api;
}
};
123 changes: 123 additions & 0 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Type } from '@sinclair/typebox';
import { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { NewProduction, Production, SdpOffer, SdpAnswer } from './models';

const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
fastify.post<{
Body: typeof NewProduction;
Reply: typeof Production;
}>(
'/production',
{
schema: {
description: 'Create a new Production resource.',
body: NewProduction,
response: {
200: Production
}
}
},
async (request, reply) => {
try {
//prod = new Production(name, [line1, line2, line3])
//Production construction will make calls to smb and set up each line.
} catch (err) {
//error handling
}
}
);

fastify.get<{
Params: { id: string };
Reply: any;
}>(
'/production',
{
schema: {
description: 'Retrieves all Production resource.',
response: {
200: Type.Array(Production)
}
}
},
async (request, reply) => {
try {
//request logic
} catch (err) {
//error handling
}
}
);

fastify.get<{
Params: { id: string };
Reply: any;
}>(
'/productions/:id',
{
schema: {
description: 'Retrieves a Production resource.',
response: {
200: Production
}
}
},
async (request, reply) => {
try {
//request logic
} catch (err) {
//error handling
}
}
);

fastify.delete<{
Params: { id: string };
Reply: string;
}>(
'/productions/:id',
{
schema: {
description: 'Delete a Production resource.',
response: {
200: Type.Object({
message: Type.Literal('removed')
})
}
}
},
async (request, reply) => {
try {
//request logic
} catch (err) {
//error handling
}
}
);

fastify.post<{
Params: { id: string; name: string };
Body: typeof SdpOffer;
Reply: typeof SdpAnswer;
}>(
'/productions/:id/lines/:name',
{
schema: {
description: 'Join a Production line.',
body: SdpOffer,
response: {
200: SdpAnswer
}
}
},
async (request, reply) => {
try {
//Generate SDP, and anything other information required to set up a connection.
} catch (err) {
//error handling
}
}
);

next();
};
Loading

0 comments on commit 76b1d24

Please sign in to comment.