-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds config and util functions
- Loading branch information
1 parent
b92f8b0
commit 5c8a942
Showing
9 changed files
with
188 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
import { env } from '../types/global.types'; | ||
import { MISSED_UPDATES_DEVELOPMENT_ROLE_ID, MISSED_UPDATES_PROD_ROLE_ID, MISSED_UPDATES_STAGING_ROLE_ID } from '../constants/commons'; | ||
import { RDS_BASE_API_URL, RDS_BASE_DEVELOPMENT_API_URL, RDS_BASE_STAGING_API_URL } from '../constants/urls'; | ||
import { env, environment } from '../types/global.types'; | ||
|
||
export const handleConfig = (env: env) => { | ||
let baseUrl: string; | ||
if (env.CURRENT_ENVIRONMENT) { | ||
if (env.CURRENT_ENVIRONMENT.toLowerCase() === 'production') { | ||
baseUrl = 'https://api.realdevsquad.com'; | ||
} else { | ||
baseUrl = 'https://staging-api.realdevsquad.com'; | ||
} | ||
} else { | ||
baseUrl = 'https://staging-api.realdevsquad.com'; | ||
} | ||
return { baseUrl }; | ||
const config = (env: env) => { | ||
const environment: environment = { | ||
production: { | ||
RDS_BASE_API_URL: RDS_BASE_API_URL, | ||
DISCORD_BOT_API_URL: env.DISCORD_BOT_API_URL, | ||
MISSED_UPDATES_ROLE_ID: MISSED_UPDATES_PROD_ROLE_ID, | ||
}, | ||
staging: { | ||
RDS_BASE_API_URL: RDS_BASE_STAGING_API_URL, | ||
DISCORD_BOT_API_URL: env.DISCORD_BOT_API_URL, | ||
MISSED_UPDATES_ROLE_ID: MISSED_UPDATES_STAGING_ROLE_ID, | ||
}, | ||
default: { | ||
RDS_BASE_API_URL: RDS_BASE_DEVELOPMENT_API_URL, | ||
DISCORD_BOT_API_URL: env.DISCORD_BOT_API_URL, | ||
MISSED_UPDATES_ROLE_ID: MISSED_UPDATES_DEVELOPMENT_ROLE_ID, | ||
}, | ||
}; | ||
|
||
return environment[env.CURRENT_ENVIRONMENT] || environment.default; | ||
}; | ||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const MISSED_UPDATES_PROD_ROLE_ID = '1183553844811153458'; | ||
export const MISSED_UPDATES_STAGING_ROLE_ID = '1183553844811153458'; | ||
export const MISSED_UPDATES_DEVELOPMENT_ROLE_ID = '1181214205081296896'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const RDS_BASE_API_URL = 'https://api.realdevsquad.com'; | ||
export const RDS_BASE_STAGING_API_URL = 'https://staging-api.realdevsquad.com'; | ||
export const RDS_BASE_DEVELOPMENT_API_URL = 'http://localhost:3000'; // If needed, modify the URL to your local API server run through ngrok | ||
|
||
export const DISCORD_BOT_API_URL = 'env'; | ||
export const DISCORD_BOT_STAGING_API_URL = ''; | ||
export const DISCORD_BOT_DEVELOPMENT_API_URL = ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { chunks } from '../utils/arrayUtils'; | ||
|
||
describe('chunks function', () => { | ||
it('should return an empty array if size is less than 1', () => { | ||
expect(chunks([1, 2, 3], 0)).toEqual([]); | ||
}); | ||
|
||
it('should return an empty array if the input array is empty', () => { | ||
expect(chunks([], 2)).toEqual([]); | ||
}); | ||
|
||
it('should split the array into chunks of the specified size', () => { | ||
const inputArray = [1, 2, 3, 4, 5, 6]; | ||
const size = 2; | ||
const expectedResult = [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]; | ||
expect(chunks(inputArray, size)).toEqual(expectedResult); | ||
}); | ||
|
||
it('should split the array into chunks of size 1 if size is not specified', () => { | ||
const inputArray = [1, 2, 3, 4, 5, 6]; | ||
const expectedResult = [[1], [2], [3], [4], [5], [6]]; | ||
expect(chunks(inputArray)).toEqual(expectedResult); | ||
}); | ||
|
||
it('should not modify the original array', () => { | ||
const inputArray = [1, 2, 3, 4, 5, 6]; | ||
const size = 2; | ||
chunks(inputArray, size); | ||
expect(inputArray).toEqual(inputArray); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,63 @@ | ||
import jwt from '@tsndr/cloudflare-worker-jwt'; | ||
|
||
import { generateJwt } from '../utils/generateJwt'; | ||
import { generateDiscordBotJwt, generateJwt } from '../utils/generateJwt'; | ||
import { privateKey } from './config/keys'; | ||
|
||
describe('Mock test', () => { | ||
describe('Generate Jwt', () => { | ||
let signSpy: jest.SpyInstance<Promise<string>>; | ||
beforeEach(() => { | ||
signSpy = jest.spyOn(jwt, 'sign'); | ||
}); | ||
afterEach(() => { | ||
signSpy.mockReset(); | ||
}); | ||
test('Generate JWT function works', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(authToken).not.toBeUndefined(); | ||
}); | ||
test('Should call sign method', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(signSpy).toBeCalledTimes(1); | ||
}); | ||
test('Should return promise without await', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(authToken).toBeInstanceOf(Promise); | ||
describe('For Rds Backend', () => { | ||
test('Generate JWT function works', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(authToken).not.toBeUndefined(); | ||
}); | ||
test('Should call sign method', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(signSpy).toBeCalledTimes(1); | ||
}); | ||
test('Should return promise without await', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }); | ||
expect(authToken).toBeInstanceOf(Promise); | ||
}); | ||
test('Throws an error if generation fails', async () => { | ||
signSpy.mockRejectedValue('Error'); | ||
await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }).catch((err) => { | ||
expect(err).toBeInstanceOf(Error); | ||
expect(err.message).toEqual('Error in generating the auth token'); | ||
}); | ||
}); | ||
}); | ||
test('Throws an error if generation fails', async () => { | ||
signSpy.mockRejectedValue('Error'); | ||
await generateJwt({ CRON_JOB_PRIVATE_KEY: privateKey }).catch((err) => { | ||
expect(err).toBeInstanceOf(Error); | ||
expect(err.message).toEqual('Error in generating the auth token'); | ||
|
||
describe('For Discord Bot', () => { | ||
test('Generate JWT function works', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = await generateDiscordBotJwt({ DISCORD_BOT_PRIVATE_KEY: privateKey }); | ||
expect(authToken).not.toBeUndefined(); | ||
}); | ||
test('Should call sign method', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
await generateDiscordBotJwt({ DISCORD_BOT_PRIVATE_KEY: privateKey }); | ||
expect(signSpy).toBeCalledTimes(1); | ||
}); | ||
test('Should return promise without await', async () => { | ||
signSpy.mockResolvedValue('Hello'); | ||
const authToken = generateDiscordBotJwt({ DISCORD_BOT_PRIVATE_KEY: privateKey }); | ||
expect(authToken).toBeInstanceOf(Promise); | ||
}); | ||
test('Throws an error if generation fails', async () => { | ||
signSpy.mockRejectedValue('Error'); | ||
await generateDiscordBotJwt({ DISCORD_BOT_PRIVATE_KEY: privateKey }).catch((err) => { | ||
expect(err).toBeInstanceOf(Error); | ||
expect(err.message).toEqual('Error in generating the auth token'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. | ||
* description credit: https://lodash.com/docs/4.17.15#chunk | ||
* source code inspiration https://youmightnotneed.com/lodash#chunk | ||
* @param {array}: array to be splitted into groups | ||
* @param {size}: size of array groups | ||
* @return {array}: array of arrays of elements split into groups the length of size. | ||
*/ | ||
export const chunks = (array: any[], size: number = 1): any[][] => { | ||
if (!Array.isArray(array) || size < 1) { | ||
return []; | ||
} | ||
const temp = [...array]; | ||
const result = []; | ||
while (temp.length) { | ||
result.push(temp.splice(0, size)); | ||
} | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters