Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic support for phone numbers api #79

Merged
merged 10 commits into from
Oct 2, 2022
1 change: 1 addition & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require('./modules/music/api/music-routes')(app); // Music
require('./modules/invoice/api/invoice-routes')(app); // Invoices
require('./modules/elements/api/elements-routes')(app); // Chemical Elements
require('./modules/time_zones/api/timezones-routes.ts')(app); // Timezones
require('./modules/phone-numbers/api/phone-numbers-routes')(app); // Phone numbers
require('./modules/quotes/api/quotes-routes')(app); // Quotes

// Add an healthcheck endpoint
Expand Down
126 changes: 63 additions & 63 deletions modules/music/api/music-routes.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { faker } from "@faker-js/faker";
import { Music } from '../consts/Music'
import { Album } from '../consts/Album'

module.exports = function (app: core.Express) {
/**
* @openapi
* '/music/{qty}':
* get:
* tags:
* - Music
* summary: (need to fix)Get a list of teams in the uk premier football league from 2022
* parameters:
* - in: path
* name: qty
* description: The amount of results you would like returned
* type: number
* default: 10
* responses:
* '200':
* description: OK
* schema:
* type: array
* items:
* type: object
* example: { teamName: '', location: '', stadium: '', capacity: ''}
*/
app.get('/music/:qty', (req: Request, res: Response) => {
const { params } = req;
let data: Music[] = [];
for (let i = 0; i <= Number(params.qty); i++) {
data.push({
id: faker.datatype.uuid(),
genre: faker.music.genre(),
song: faker.music.songName(),
});
}
res.json({
data,
total: data.length
})
})

app.get("/album/:qty", (req: Request, res: Response) => {
const {params} = req
let data: Album[] = []

data.push({
id: faker.datatype.uuid(),
name: faker.music.songName(),
releasedate: faker.date.past(80).toString().split(' ')[3],
price: "$" + Math.floor(Math.random() * (20 - 10 + 1) + 10),
publisher: faker.company.name(),
songs: Number(params.qty)
})

res.json({
data
})
})

import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { faker } from "@faker-js/faker";
import { Music } from '../consts/Music'
import { Album } from '../consts/Album'
module.exports = function (app: core.Express) {
/**
* @openapi
* '/music/{qty}':
* get:
* tags:
* - Music
* summary: (need to fix)Get a list of teams in the uk premier football league from 2022
* parameters:
* - in: path
* name: qty
* description: The amount of results you would like returned
* type: number
* default: 10
* responses:
* '200':
* description: OK
* schema:
* type: array
* items:
* type: object
* example: { teamName: '', location: '', stadium: '', capacity: ''}
*/
app.get('/music/:qty', (req: Request, res: Response) => {
const { params } = req;
let data: Music[] = [];
for (let i = 0; i <= Number(params.qty); i++) {
data.push({
id: faker.datatype.uuid(),
genre: faker.music.genre(),
song: faker.music.songName(),
});
}
res.json({
data,
total: data.length
})
})
app.get("/album/:qty", (req: Request, res: Response) => {
const {params} = req
let data: Album[] = []
data.push({
id: faker.datatype.uuid(),
name: faker.music.songName(),
releasedate: faker.date.past(80).toString().split(' ')[3],
price: "$" + Math.floor(Math.random() * (20 - 10 + 1) + 10),
publisher: faker.company.name(),
songs: Number(params.qty)
})
res.json({
data
})
})
}
96 changes: 96 additions & 0 deletions modules/phone-numbers/api/phone-numbers-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import e, { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import { faker } from "@faker-js/faker";
import countryNumberData from '../consts/countryNumberData';
import supportedCountries from '../utils/supportedCountries';
import formats from '../consts/formats';

module.exports = function(app : core.Express) {

/**
* @openapi
* '/phonenumbers':
* get:
* tags:
* - Phone Numbers
* summary: Obtain a random phone number in a random format
* responses:
* '200':
* description: OK
* schema:
* type: string
* example: "+1 222 333 4444"
*/

// return a number phone number in a random format
app.get("/phonenumbers", (req: Request, res: Response) => {
res.json(faker.phone.number())
})

/**
* @openapi
* '/phonenumbers/country/{countrycode}/{format}':
* get:
* tags:
* - Phone Numbers
* summary: Obtain a random phone number based on country code and optional formatting
* parameters:
* - in: path
* name: country code
* description: The 2 letter code of a country (ISO 3166-1 alpha-2)
* required: true,
* type: string
* - in: path
* name: format
* description: The separator for the phone number 'space', 'nospace', 'dash'
* default: space
* type: string
* required: false
* responses:
* '200':
* description: OK
* schema:
* type: string
* example: "+1-222-333-4444"

*/

// return a number based on the 2 letter country code (ISO 3166-1)
// format can be "space", "nospace", or "dash"
// format will be "space" by default
app.get("/phonenumbers/country/:cc/:format?", (req: Request, res: Response) => {
const countryData = countryNumberData[req.params.cc.toUpperCase()];

// default format to be space
const separator = formats[req.params.format] === undefined ? formats['space'] : formats[req.params.format];

if (countryData === undefined) {
return res.status(400).json("Invalid country code please use 2 letter country code (ISO 3166-1)");
} else if (countryData['format'] === undefined) {
return res.status(400).json("Country currently not supported, supported countries: " + JSON.stringify(supportedCountries));
} else {
res.json(faker.phone.number(countryData['format']).split(' ').join(separator))
}
})

/**
* @openapi
* '/phonenumbers/imei':
* get:
* tags:
* - Phone Numbers
* summary: Obtain a random IMEI (International Mobile Equipment Identity) number
* responses:
* '200':
* description: OK
* schema:
* type: string
* example: "85-635465-967419-2"
*/

// return a random IMEI number
app.get("/phonenumbers/imei", (req: Request, res: Response) => {
res.json(faker.phone.imei())
})

}