Skip to content

Commit f02e19f

Browse files
Extract id-making
1 parent ecdec76 commit f02e19f

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

src/lib/openapi/fakes/users-endpoint.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { makeId } from '$lib/utils';
12
import {
23
Rank,
34
type ApiResponse,
@@ -129,9 +130,9 @@ export default class FakeUsersEndpointApi implements UsersEndpointApiInterface {
129130
throw new Error('Method not implemented.');
130131
}
131132

132-
private makeUser(username: string) {
133+
private makeUser(username: string): User {
133134
return {
134-
id: Math.random().toString(36).substring(2),
135+
id: makeId(),
135136
dateCreated: new Date(),
136137
username: username,
137138
rank: Rank.Citizen,

src/lib/openapi/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { dev } from '$app/environment';
22
import { api } from '$lib/data/urls.json';
33
import { getStoredItem, setStoredItem } from '$lib/storage';
44
import { DisplayableError, eventBus } from '$lib/events';
5-
import { sleep } from '$lib/utils';
5+
import { makeId, sleep } from '$lib/utils';
66
import {
77
Configuration,
88
FetchError,
@@ -94,7 +94,7 @@ class RequestIdMiddleware implements Middleware {
9494
const headers = { ...context.init.headers } as Record<string, string>;
9595

9696
if (!headers[RequestIdMiddleware.headerName]) {
97-
headers[RequestIdMiddleware.headerName] = Math.random().toString(36).substring(2);
97+
headers[RequestIdMiddleware.headerName] = makeId();
9898
}
9999

100100
context.init.headers = headers;

src/lib/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export function sleep(ms: number): Promise<void> {
22
return new Promise((resolve) => setTimeout(resolve, ms));
33
}
4+
5+
export function makeId(): string {
6+
return Math.random().toString(36).substring(2);
7+
}

0 commit comments

Comments
 (0)