Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Last active May 5, 2020 07:27
Show Gist options
  • Save phatnguyenuit/90500b9f6432ae6064af6ef2cd8916d0 to your computer and use it in GitHub Desktop.
Save phatnguyenuit/90500b9f6432ae6064af6ef2cd8916d0 to your computer and use it in GitHub Desktop.
Typescript Code Snippets
const asyncTask = (fakeResponse: object, ms: number) =>
new Promise(resolve => setTimeout(() => resolve(fakeResponse), ms));
const faker = async (data?: BaseRequestConfig) => {
const {
params: { page = 0, pageSize = 10 },
} = data || {};
const response: BaseResponse<SmsRequestListingData> = {
code: 2e5,
kind: 'success',
message: 'Yêu cầu thực hiện thành công',
data: {
page,
pageSize,
records: Array(pageSize)
.fill('')
.map((_, index) => ({
smsId: (page * pageSize + (index + 1)).toString(),
createdAt: dayjs()
.subtract(index + 1, 'day')
.valueOf()
.toString(),
phone: `09${Math.random()
.toFixed(8)
.slice(2)}`,
telcoId: randomBetween(1, 5).toString(),
providerId: randomBetween(1, 5).toString(),
serviceId: randomBetween(1, 5).toString(),
status: randomChoice([
'completed',
'processing',
'failed',
]).toUpperCase(),
updatedAt: dayjs()
.subtract(index + 1, 'hour')
.valueOf()
.toString(),
providerMsg: `Provider Msg ${index + 1}`,
})),
total: 100,
},
};
const fakeResponse = await asyncTask(response, 500);
return fakeResponse;
};
const randomChoice = <TItem>(lst: Array<TItem>) =>
lst[Math.floor(Math.random() * lst.length)];
const randomBetween = (min: number, max: number) =>
Math.floor(Math.random() * (max - min + 1)) + min;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment