Skip to content

Commit

Permalink
feat: added jwt component to AbstractController
Browse files Browse the repository at this point in the history
feat: update dependencies uWebSockets to 20.48.0
feat: set version 1.1.6
  • Loading branch information
sanchezzzhak committed Aug 29, 2024
1 parent 0f1b75a commit 6d9b6af
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 35 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-moleculer-web",
"version": "1.1.5",
"version": "1.1.6",
"description": "Fast web app service for moleculer.js + uWebSockets.js",
"main": "src/index.js",
"repository": {
Expand All @@ -15,7 +15,7 @@
"ejs": "^3.1.10",
"moleculer": "^0.14.32",
"qs": "^6.12.1",
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.44.0"
"uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.48.0"
},
"engines": {
"node": ">= 20.x",
Expand Down
78 changes: 63 additions & 15 deletions src/abstract-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const HTTP_CODES = require('./utils/http-codes');
const Timer = require("./utils/timer");
const RequestData = require("./request-data");
const CookieData = require("./cookie-data");

const JWT = require("./utils/jwt");

/** @typedef {import("uWebSockets.js").HttpRequest} HttpRequest */
/** @typedef {import("uWebSockets.js").HttpResponse} HttpResponse */
Expand Down Expand Up @@ -43,25 +43,27 @@ const readBody = (res, cb, err) => {
};

class AbstractController {

/** @type {RequestData|null} */
requestData = null;
/** @type {CookieData|null} */
cookieData = null;
/** default format mime type response */
format = 'html';
/** default status number response */
statusCode = 200;
/** default status text response */
statusCodeText = '200 OK';
/** headers to response */
headers = {};
/** @type {HttpRequest} res */
req;
/** @type {HttpResponse} res */
res;
/** @type {ServiceBroker} broker */
broker;

/** */
/** request client-hints for header response */
clientHints = false;

/** redirect type for the redirect method */
redirectType = REDIRECT_TYPES.REDIRECT_TYPE_META;

constructor(opts = {}) {
Expand All @@ -73,6 +75,47 @@ class AbstractController {

}

/**
* Create JWT token for payload data
* @param {{}} payload
* @return {string}
*/
createJwtToken(payload = {}) {
return this.getJWT().create(payload);
}

/**
* Extract jwt token to payload data
* @param token
* @return {*}
*/
extractJwtToken(token) {
return this.getJWT().extract(token);
}

/**
* Get JWT component
* @return {JWT}
*/
getJWT() {
if (!this.jwt) {
throw new Error('To use this method you need to call the initJWT(key, iat) method') ;
}
return this.jwt;
}

/**
* Init JWT component to property
* @param {string} key
* @param {boolean} iat
*/
initJWT(key, iat = false) {
this.jwt = new JWT({key, iat});
}

/**
* Init requestData and cookieData components to properties
*/
initRequest() {
this.requestData = new RequestData(this.req, this.res);
this.cookieData = new CookieData(this.req, this.res);
Expand All @@ -81,7 +124,7 @@ class AbstractController {
}
}
/**
* remove unnecessary information from the validators from the array
* Remove unnecessary information from the validators from the array
* @param {[{field:"", message:""}]} listErrors
* @returns {[]}
*/
Expand All @@ -95,20 +138,25 @@ class AbstractController {
}

/**
* final response as JSON
* Final response as JSON
* @param {JSONObject} obj
* @param {number} httpCode
*/
asJson(obj, httpCode = 200) {
return this.renderRaw({view: JSON.stringify(obj), httpCode, format: 'json'});
}

/**
* Write header to response
* @param key
* @param value
*/
writeHeader(key, value) {
this.headers[key] = value;
}

/**
* Set all cors
* Write all cors headers allow to response
*/
setCorsHeaders() {
this.writeHeader('Access-Control-Allow-Origin', '*');
Expand All @@ -120,7 +168,7 @@ class AbstractController {
}

/**
* Set headers client-hints
* Write headers client-hints to response
*/
setClientHintsHeaders() {
this.writeHeader('accept-ch', [
Expand All @@ -135,15 +183,15 @@ class AbstractController {
}

/**
* is current connect aborted
* Is current connect aborted
* @return {any}
*/
isAborted() {
return !!this.res.aborted;
}

/**
* read post data
* Read post data
* @returns {Promise<unknown>}
*/
readBody() {
Expand All @@ -153,7 +201,7 @@ class AbstractController {
}

/**
* render as text
* Render as text
* @param {string} view
* @param {number|null} httpCode
* @param {string|null} format
Expand All @@ -172,7 +220,7 @@ class AbstractController {
}

/**
* render ejs template
* Render ejs template
* @param {string} template
* @param {{}} params
* @param {number} httpCode
Expand All @@ -185,7 +233,7 @@ class AbstractController {
}

/**
* set http status
* Set http status
* @param {number} httpCode
*/
setStatus(httpCode) {
Expand All @@ -194,7 +242,7 @@ class AbstractController {
}

/**
* redirect
* Redirect
* @param {string} location
* @param {number} httpCode
*/
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type PortSchemaOption = "node" | "auto";

export interface RouteOptions {
path: string;
method: RouteOptionMethod;
method: RouteOptionMethod | string;
controller?: string;
action?: string;
service?: string;
Expand Down
13 changes: 12 additions & 1 deletion src/utils/http-codes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// https://ru.wikipedia.org/wiki/%D0%A1%D0%BF%D0%B8%D1%81%D0%BE%D0%BA_%D0%BA%D0%BE%D0%B4%D0%BE%D0%B2_%D1%81%D0%BE%D1%81%D1%82%D0%BE%D1%8F%D0%BD%D0%B8%D1%8F_HTTP
const HTTP_CODES = {
200: '200 OK',
201: '201 Created',
202: '202 Accepted',
203: '203 Non-Authoritative Information',
204: '204 No Content',
205: '205 Reset Content',
304: '304 Not Modified',
302: '302 Found', // http/1.1
303: '303 See Other',
Expand All @@ -10,7 +15,13 @@ const HTTP_CODES = {
401: '401 Unauthorized',
403: '403 Forbidden',
404: '404 Not Found',
406: '405 Method Not Allowed'
406: '405 Method Not Allowed',
409: '409 Conflict',
500: '500 Internal Server Error',
501: '501 Not Implemented',
502: '502 Bad Gateway',
503: '503 Service Unavailable',
504: '504 Gateway Timeout',
};

module.exports = HTTP_CODES;
Loading

0 comments on commit 6d9b6af

Please sign in to comment.