@traderapp/shared-resources

v2.0.1

Published

This module serves as a collection of functions and types/interfaces designed for reuse throughout our codebase.

Downloads

80

Readme

@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,
  initializeLogger,
  LoggerConfig,
  logger,
  initSecrets,
  apiDocumentationResponseObject,
  QueueMessage,
  QueueClient
} 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;
}

3. LoggerConfig

interface LoggerConfig {
	logGroupName: string;
	region: string;
	accessKeyId: string;
	secretAccessKey: string;
	uploadFreq?: number;
	local?: boolean;
}

4. QueueMessage

 interface QueueMessage<T = any> extends Omit<Message, "Body"> {
	Body: T;
}

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 QueueMessage:

import { QueueMessage } from "@traderapp/shared-resources";

const processedMessages: QueueMessage<IQueueMessage>[] = [];

Example using logger:

import { logger, initializeLogger, LoggerConfig } from "@traderapp/shared-resources";

// initializing logger in app.ts
	const config: LoggerConfig = {
		logGroupName: "your Log Group Name",
		region: "your aws region",
		accessKeyId: "your aws access key id",
		secretAccessKey: "your aws secret access key",
		uploadFreq: 10000, // Optional
		local: false // optional
	};
	initializeLogger(config);

// logging with different logger levels
logger.log("something", { name: 'Kane', surname: 'King'} );
logger.error("write error message", "errors");
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
  },
};

Example using QueueClient:

import { QueueClient } from "@traderapp/shared-resources";

const sqsClient = new QueueClient({ region, queueUrl });

// Sending message
await sqsClient.sendMessage(message);

// Recieving message from Queue
const messages = await sqsClient.receiveMessages({
			maxNumberOfMessages: Number
		});

// Deleting message 
await sqsClient.deleteMessages(messages);

Example using QueueMessage:

import { QueueMessage } from "@traderapp/shared-resources";

const processedMessages: QueueMessage<IQueueMessage>[] = [];