Skip to content

Commit d46a7da

Browse files
committed
styles: test with prettier and adding configs
1 parent 5def8e3 commit d46a7da

File tree

5 files changed

+31
-42
lines changed

5 files changed

+31
-42
lines changed

src/core/fetcher.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { DataCategory } from "./types.js";
22
import { SourceFetchError } from "./errors.js";
33
import axios from "axios";
4-
5-
const BASE_URL = "https://jsonplaceholder.typicode.com";
4+
import { BASE_URL } from "../utils/config.js";
65

76
export async function fetchData(category: DataCategory) {
87
try {

src/core/generator.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,12 @@ export async function generate(options: MockmateOptions) {
66
let result: any[] = [];
77

88
if (options.schema) {
9-
result = generateFromSchema(
10-
options.schema,
11-
options.quantity ?? 1
12-
);
13-
}
14-
15-
else if (options.category) {
9+
result = generateFromSchema(options.schema, options.quantity ?? 1);
10+
} else if (options.category) {
1611
const data = await fetchData(options.category);
17-
result = options.quantity
18-
? data.slice(0, options.quantity)
19-
: data;
20-
}
21-
22-
else {
23-
throw new Error(
24-
'Mockmate: provide either "schema" or "category"'
25-
);
12+
result = options.quantity ? data.slice(0, options.quantity) : data;
13+
} else {
14+
throw new Error('Mockmate: provide either "schema" or "category"');
2615
}
2716

2817
if (options.pick) {
@@ -41,10 +30,7 @@ export async function generate(options: MockmateOptions) {
4130
result = result.map((item: Record<string, unknown>) => ({
4231
...item,
4332
...Object.fromEntries(
44-
Object.entries(options.extend!).map(([key, fn]) => [
45-
key,
46-
fn(),
47-
])
33+
Object.entries(options.extend!).map(([key, fn]) => [key, fn()])
4834
),
4935
}));
5036
}

src/core/shema.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import type { SchemaDefenititon } from "./types";
22

3-
export function generateFromSchema(schema: SchemaDefenititon, quantity: number = 1) {
4-
const result = [];
3+
export function generateFromSchema(
4+
schema: SchemaDefenititon,
5+
quantity: number = 1
6+
) {
7+
const result = [];
58

6-
for (let i = 0; i < quantity; i++) {
7-
const item: Record<string, unknown> = {};
9+
for (let i = 0; i < quantity; i++) {
10+
const item: Record<string, unknown> = {};
811

9-
for (const key in schema) {
10-
item[key] = schema[key]();
11-
}
12-
result.push(item)
12+
for (const key in schema) {
13+
item[key] = schema[key]();
1314
}
15+
result.push(item);
16+
}
1417

15-
return result;
18+
return result;
1619
}

src/examples/generate.users.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { mockmate } from "../index.js";
22

33
async function run() {
4-
const users = await mockmate({
5-
category: "users",
6-
quantity: 2,
7-
extend: {
8-
zipcode: () => "90566 - 7771"
9-
},
10-
});
4+
const users = await mockmate({
5+
category: "users",
6+
quantity: 2,
7+
extend: {
8+
zipcode: () => "90566 - 7771",
9+
},
10+
});
1111

12-
console.groupCollapsed("Generated users: ");
13-
console.table(users);
14-
console.groupEnd();
15-
console.log(users);
12+
console.groupCollapsed("Generated users: ");
13+
console.table(users);
14+
console.groupEnd();
15+
console.log(users);
1616
}
1717

1818
run();

src/utils/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const BASE_URL = "https://jsonplaceholder.typicode.com";

0 commit comments

Comments
 (0)