Skip to content

Commit

Permalink
Merge branch 'release/R.6.2.0-Api-Versioning-Prefix'
Browse files Browse the repository at this point in the history
  • Loading branch information
johnliveeoroncillo committed Apr 17, 2022
2 parents e4112fb + 2bb404f commit d24b2eb
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ sample-api: (API Key)
method: post (API Method)
enabled: true (Enable/Disable option)
middleware: authorizer (File name of the middleware)
version: 1 (Optional: Version of the api)
prefix: api (Optional: API prefix)
```
With Version: /v1/sample-api
With Prefix: /api/sample-api
With Prefix and Version: /api/v1/sample-api

#### ./cron/sample-cron/config.yml
```
Expand Down
17 changes: 14 additions & 3 deletions code_templates/ApiTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function execute(req: HttpRequest, res: Response): Promise<HttpResp
},
res,
);
} catch(e) {
} catch (e) {
return API_RESPONSE(e, res);
}
// finally {
Expand Down Expand Up @@ -115,8 +115,19 @@ const config = `<key_name>:
endpoint: /<endpoint>
method: <method>
enabled: true
#UNCOMMENT TO ATTACH MIDDLEWARE
#EXAMPLE: middleware: middleware
#ADD API VERSION
#EXAMPLE OUTPUT: /login to /v1/login
#version: 1
#ADD API PREFIX
#EXAMPLE: api
#EXAMPLE OUTPUT: /login to /api/login
#WORKS WITH VERSION SAMPLE OUTPUT: /api/v1/login
#prefix: api
#ADD MIDDLEWARE
#EXAMPLE: middleware: authorizer
#middleware: <middleware_name>
`;

Expand Down
16 changes: 0 additions & 16 deletions core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ const routes: Config[] = [];
const listRoutes = require('express-list-routes');

const loadRoutes = (dir = ''): Promise<Config[]> => {
// if (!dir) dir = `${__dirname}/../src/functions/apis`;

// return new Promise((resolve) => {
// fs.readdirSync(dir).forEach((file: string) => {
// const absolute = path.join(dir, file);

// if (fs.statSync(absolute).isDirectory()) {
// if (fs.existsSync(absolute)) {
// const config = getConfig(`${absolute}/config.yml`);
// if (config) routes.push(config);
// }
// }
// });

// return resolve(routes);
// });
return new Promise((resolve) => {
glob(`${__dirname}/../src/functions/apis/**/*.yml`, (er: any, files: any) => {
if (files?.length) {
Expand Down
2 changes: 2 additions & 0 deletions core/libs/ApiEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface RouteConfig {
middleware?: string;
handler: string;
enabled: boolean;
version?: string;
prefix?: string;

timezone?: string;
autostart?: boolean;
Expand Down
19 changes: 17 additions & 2 deletions core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,32 @@ loadRoutes().then(async (routes) => {

const enabled = route?.enabled ?? false;
if (enabled) {
const endpoint = route.endpoint.replace(/{/g, ':').replace(/}/g, '');
let endpoint = route.endpoint.replace(/{/g, ':').replace(/}/g, '');
endpoint = `<prefix><version>${endpoint}`;
const handler = route.handler;
const method = METHODS?.[route.method] ?? '';
const middleware = route.middleware;
// const middleware = route.middleware
const prefix = route?.prefix ?? '';
const version = route?.version ?? '';

Logger.info('Test', route);

const { execute } = await import(`.${handler}`);
const callbacks = [];
if (middleware) {
const { execute } = await import(`../src/functions/middlewares/${middleware}`);
callbacks.push(execute);
}

/**
* MODIFY ENDPOINT SETTINGS
*/
if (prefix && prefix !== '') endpoint = endpoint.replace(/<prefix>/g, `/${prefix}`);
else endpoint = endpoint.replace(/<prefix>/g, '');

if (version && version !== '') endpoint = endpoint.replace(/<version>/g, `/v${version}`);
else endpoint = endpoint.replace(/<version>/g, '');

callbacks.push(execute);
app[method](endpoint, callbacks);
}
Expand Down
19 changes: 19 additions & 0 deletions src/functions/apis/versioning_test/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
versioning_test:
handler: ./src/functions/apis/versioning_test/handler
endpoint: /version
method: get
enabled: true

#ADD API VERSION
#EXAMPLE OUTPUT: /login to /v1/login
version: 1

#ADD API PREFIX
#EXAMPLE: api
#EXAMPLE OUTPUT: /login to /api/login
#WORKS WITH VERSION SAMPLE OUTPUT: /api/v1/login
prefix: api

#ADD MIDDLEWARE
#EXAMPLE: middleware: authorizer
#middleware: <middleware_name>
23 changes: 23 additions & 0 deletions src/functions/apis/versioning_test/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { API_RESPONSE } from '../../../../core';
import { HttpResponse, HttpRequest } from '../../../../core/libs/ApiEvent';
import { Response } from 'express';
import { Mysql } from '../../../../core/databases/Mysql';
import { Connection } from 'typeorm';

import { Response200 } from './response';

export async function execute(req: HttpRequest, res: Response): Promise<HttpResponse> {
try {
return API_RESPONSE(
{
...Response200.SUCCESS,
},
res,
);
} catch (e) {
return API_RESPONSE(e, res);
}
// finally {
// await Mysql.closeConnection();
// }
}
15 changes: 15 additions & 0 deletions src/functions/apis/versioning_test/response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpResponse } from '../../../../core/libs/ApiEvent';

/*
Your Custom Response */
export class Response200 {
static SUCCESS: HttpResponse = {
code: 200,
message: 'Success',
};
}

export class NotFound {
code = 404;
message = 'Username not found';
}

0 comments on commit d24b2eb

Please sign in to comment.