Skip to content

Commit

Permalink
Merge branch 'develop' into deepsource-autofix-7fc1aff8
Browse files Browse the repository at this point in the history
  • Loading branch information
Viserion77 committed Jan 26, 2024
2 parents 074dc9a + 022c2f9 commit 57f1bde
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[![DeepSource](https://app.deepsource.com/gh/Adapcon/adapcon-utils-js.svg/?label=active+issues&show_trend=true&token=z9o0q95BRMd2RWNUS_SdBYs0)](https://app.deepsource.com/gh/Adapcon/adapcon-utils-js/?ref=repository-badge)
[![DeepSource](https://app.deepsource.com/gh/Adapcon/adapcon-utils-js.svg/?label=resolved+issues&show_trend=true&token=z9o0q95BRMd2RWNUS_SdBYs0)](https://app.deepsource.com/gh/Adapcon/adapcon-utils-js/?ref=repository-badge)
11 changes: 11 additions & 0 deletions __tests__/files/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getExtensionFromMimeType } from '../../src/files'

describe('Files', () => {
it('Should correct file extension from mimeType', () => {
expect(getExtensionFromMimeType('video/mp4')).toEqual('mp4')
})

it('Should return null as mimeType is unkown', () => {
expect(getExtensionFromMimeType('log/banana')).toBeNull()
})
})
14 changes: 14 additions & 0 deletions __tests__/lambda/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ describe('invoke', () => {
})).toEqual({ body: { error: 'Invalid Session' }, status: 401 })
})

it('Should returns response formatted with headers', () => {
expect(formattedResponse({
StatusCode: 200,
Payload: '{"headers": {"total-count":"20"}}'
})).toEqual({ headers: { 'total-count': '20' }, status: 200, body: {} })
})

it('Should returns response formatted with headers and body', () => {
expect(formattedResponse({
StatusCode: 200,
Payload: '{"headers": {"total-count":"20"},"body": "{\\"name\\":\\"John Doe\\"}"}'
})).toEqual({ headers: { 'total-count': '20' }, status: 200, body: { name: 'John Doe' } })
})

it('Should returns response formatted with status code 200', () => {
expect(formattedResponse({
StatusCode: 200,
Expand Down
18 changes: 18 additions & 0 deletions __tests__/lambda/lambdaGetParameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ describe('lambdaGetParameters', () => {
date: 1639423222426
}
},
{
event: {
pathParameters: {
auth: 'xyz',
app: 'theAppId'
},
queryStringParameters: null
},
eventParams: {
auth: 'pathParameters',
app: 'pathParameters',
date: 'queryStringParameters'
},
output: {
auth: 'xyz',
app: 'theAppId'
}
},
{
event: {
pathParameters: {
Expand Down
14 changes: 13 additions & 1 deletion __tests__/lambda/lambdaResponses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ describe('lambdaResp', () => {
statusCode: 404,
body: 'not found!'
}
},
{
input: {
statusCode: 200,
body: '',
headers: { 'total-count': 42069 }
},
output: {
statusCode: 200,
body: '',
headers: { 'total-count': 42069 }
}
}
]

test.each(data)('Should return an response Object, with statusCode and body props', (param) => {
expect(lambdaResp(param.input.statusCode, param.input.body)).toStrictEqual(param.output)
expect(lambdaResp(param.input.statusCode, param.input.body, param.input.headers)).toStrictEqual(param.output)
})
})

Expand Down
43 changes: 43 additions & 0 deletions __tests__/string/modifiers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { stringToArray } from '../../src/string'

describe('stringToArray', () => {
const data = [
{
output: []
},
{
delimiter: ',',
output: []
},
{
input: 'id,style,appId',
output: ['id', 'style', 'appId']
},
{
input: 'id, style, appId',
output: ['id', 'style', 'appId']
},
{
input: '',
output: []
},
{
input: 'id',
output: ['id']
},
{
input: 'id',
delimiter: ',',
output: ['id']
},
{
input: 'id;style;appId',
delimiter: ';',
output: ['id', 'style', 'appId']
}
]

test.each(data)('Should return array of string', (param) => {
expect(stringToArray(param.input ? param.input : undefined, param.delimiter ? param.delimiter : undefined)).toStrictEqual(param.output)
})
})
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adapcon-utils-js",
"version": "0.15.1",
"version": "0.16.7",
"description": "Utils library for Javascript",
"keywords": [],
"author": {
Expand Down Expand Up @@ -57,6 +57,6 @@
"typescript": "^4.4.2"
},
"engines": {
"node": "14.x"
"node": ">=14.x"
}
}
28 changes: 28 additions & 0 deletions src/files/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export enum FILE_EXTENSIONS {
'video/mp4' = 'mp4',
'audio/ogg; codecs=opus' = 'ogg',
'image/gif' = 'gif',
'image/jpeg' = 'jpg',
'image/svg+xml' = 'svg',
'image/tiff' = 'tiff',
'image/png' = 'png',
'image/bmp' = 'bmp',
'text/html' = 'html',
'text/csv' = 'csv',
'text/css' = 'css',
'text/plain' = 'txt',
'text/xml' = 'xml',
'text/tab-separated-values' = 'tsv',
'application/pdf' = 'pdf',
'application/xml' = 'xml',
'application/zip' = 'zip',
'application/x-compressed-zip' = 'zip',
'application/vnd.ms-excel' = 'xls',
'application/x-bzip2' = 'bz2',
'application/msword' = 'doc',
'application/x-gzip' = 'gz',
'application/java-archive' = 'jar',
'application/x-javascript' = 'js',
'application/vnd.ms-powerpoint' = 'ppt',
'application/x-tar' = 'tar.gz'
}
2 changes: 2 additions & 0 deletions src/files/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './extensions'
export * from './utils'
5 changes: 5 additions & 0 deletions src/files/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FILE_EXTENSIONS } from '.'

export function getExtensionFromMimeType (mimeType: string): string | null {
return FILE_EXTENSIONS[mimeType] || null
}
2 changes: 2 additions & 0 deletions src/http/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export enum HttpStatuses {
created = 201,
accepted = 202,
noContent = 204,
partialContent = 206,
badRequest = 400,
unauthorized = 401,
invalidSession = 401,
forbidden = 403,
notFound = 404,
userNotFound = 404,
integrationError = 406,
preconditionFailed = 412,
teaPot = 418,
unprocessableEntity = 422,
internalError = 500,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './date'
export * from './dynamoose'
export * from './email'
export * from './error'
export * from './files'
export * from './http'
export * from './image'
export * from './invoice'
Expand Down
5 changes: 3 additions & 2 deletions src/lambda/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import jsonBigInt from 'json-bigint'

import { isObject } from '../object'

export const formattedResponse = ({ StatusCode, Payload }: { StatusCode?: any, Payload?: any }): object => {
export const formattedResponse = ({ StatusCode, Payload }: { StatusCode?: number, Payload?: any }): object => {
const payloadFormatted = JSON.parse(Payload || '{}')

return {
status: payloadFormatted.statusCode || StatusCode,
body: payloadFormatted.body ? JSON.parse(payloadFormatted.body) : {}
body: payloadFormatted.body ? JSON.parse(payloadFormatted.body) : {},
...(payloadFormatted.headers ? { headers: payloadFormatted.headers } : null)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lambda/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface Error {
message?: string
}

export type Headers = {
[header: string]: string | number | boolean
}

export interface lambdaParameters {
port?: string
region?: string
Expand Down
3 changes: 2 additions & 1 deletion src/lambda/lambdaGetParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { kebabCaseToCamelCase } from './../string/formatters'
export const lambdaGetParameters = (event: object, eventParams: object): { [key: string]: any } => {
const fedParams = {}

for (const element in eventParams) {
for (const element of Object.keys(eventParams)) {
const path: string = eventParams[element]

if (path in event) {
const eventPathObject: object = event[path]
if (!eventPathObject) continue
const fedParam: any = path === 'body' ? eventPathObject : eventPathObject[element]
let paramKey: string = element
if (path === 'headers') {
Expand Down
7 changes: 4 additions & 3 deletions src/lambda/lambdaResponses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { isNumber } from '../number'
import { objToStr } from '../object'
import { ProxyResult } from 'aws-lambda'
import type { Error } from './interfaces'
import type { Error, Headers } from './interfaces'

export const lambdaResp = (statusCode: number, body?: object | string): ProxyResult => ({
export const lambdaResp = (statusCode: number, body?: object | string, headers?: Headers): ProxyResult => ({
statusCode,
...(body ? { body: objToStr(body) } : { body: '' })
...(body ? { body: objToStr(body) } : { body: '' }),
...(headers ? { headers } : null)
})

export const lambdaRespError = (err: Error): ProxyResult => {
Expand Down
1 change: 1 addition & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './validators'
export * from './formatters'
export * from './modifiers'
4 changes: 4 additions & 0 deletions src/string/modifiers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const stringToArray = (str = '', delimiter = ','): string[] => {
if (typeof str !== 'string' || str === '') return []
return str.split(delimiter).map((value) => value.trim())
}

0 comments on commit 57f1bde

Please sign in to comment.