1.0.5 • Published 6 months ago
@traderapp/shared-resources v1.0.5
@traderapp/shared-resources
This module serves as a collection of functions and types/interfaces designed for reuse throughout our codebase.
Quickstart
To install the package, use npm:
1. Install
npm install @traderapp/shared-resources
2. imports
import {
APIResponse,
APIResponseInput,
apiResponseHandler,
Logger,
initSecrets,
apiDocumentationResponseObject,
} from "@traderapp/shared-resources";
Types/Interfaces
1. APIResponseInput
interface APIResponseInput {
type?: string;
object?: object | null;
message?: string;
}
2. APIResponse
interface APIResponse {
data: object | null;
error: object | null;
message: string;
}
Example using APIReponse
:
import { APIReponse } from "@traderapp/shared-resources";
const response: APIReponse = {
data: null || {},
error: null,
message: "Request was successfull",
}
Example using APIResponseInput
:
import { APIResponseInput } from "@traderapp/shared-resources";
export default function apiResponseFunction(params?: APIResponseInput) {
}
Example using logger
:
import { logger } from "@traderapp/shared-resources";
logger.log("something");
logger.error("write error message");
logger.debug("debug message");
logger.warn("warning message");
logger.info("show info");
Example using apiResponseHandler
:
import { apiResponseHandler } from "@traderapp/shared-resources";
const getUser = async (req, res) => {
res.status(200).json(apiResponseHandler({
object: {},
message: '',
}));
};
Example using initSecrets
:
// app.ts
import { initSecrets } from "@traderapp/shared-resources";
import secretsJson from './secret.json';
(async function() {
await initSecrets({
env: 'dev',
secretNames: [
'first-secret',
'second-secret',
],
secretsJson: secretsJson,
});
})();
Example using swaggerResponse
:
import { apiDocumentationResponseObject } from "@traderapp/shared-resources";
apiDocumentationResponseObject(description: string)
//constants.ts
export const DOC_RESPONSE = {
SERVERERROR: apiDocumentationResponseObject('Internal Server Error'),
UNAUTHORIZED: apiDocumentationResponseObject('Error: Unauthorized'),
BADREQUEST: apiDocumentationResponseObject('Error: Bad Request'),
SUCCESS: apiDocumentationResponseObject('Success'),
}
//documentation/filename.docs.ts
import { DOC_RESPONSE } from "../config/constants";
const createFilename = {
tags: [],
description: '',
requestBody: {
},
responses: {
[RESPONSE_CODES.badRequest]: DOC_RESPONSE.BADREQUEST,
[RESPONSE_CODES.unauthorized]: DOC_RESPONSE.UNAUTHORIZED,
[RESPONSE_CODES.serverError]: DOC_RESPONSE.SERVERERROR
[RESPONSE_CODES.SUCCESS]: DOC_RESPONSE.SUCCESS
},
};