-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathref.ts
41 lines (34 loc) · 1.17 KB
/
ref.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as prismic from "@prismicio/client";
import { capitalCase } from "../lib/changeCase";
import { createFaker } from "../lib/createFaker";
import { MockRestApiConfig } from "../types";
import { timestamp } from "../value";
export type MockRestApiRefConfig<IsScheduled extends boolean = false> = {
isMasterRef?: boolean;
isScheduled?: IsScheduled;
} & MockRestApiConfig;
export type MockRestApiRefValue<IsScheduled extends boolean = false> = Omit<
prismic.Ref,
"scheduledAt"
> &
(IsScheduled extends true
? { scheduledAt: string }
: { scheduledAt?: never });
export const ref = <IsScheduled extends boolean = false>(
config: MockRestApiRefConfig<IsScheduled>,
): MockRestApiRefValue<IsScheduled> => {
const faker = config.faker || createFaker(config.seed);
const value: prismic.Ref = {
id: faker.hash(16),
ref: faker.hash(16),
isMasterRef: config.isMasterRef ?? false,
label: config.isMasterRef
? "Master"
: capitalCase(faker.words(faker.range(1, 3))),
};
if (config.isScheduled) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
value.scheduledAt = timestamp({ faker })!;
}
return value as MockRestApiRefValue<IsScheduled>;
};