Skip to content

Commit 48e3efe

Browse files
committed
feat: sources to parse json data
1 parent 2f68e7f commit 48e3efe

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mockmate/mockmate",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"author": "monokkai",
55
"license": "MIT",
66
"main": "dist/index.js",

src/core/fetcher.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { DataCategory } from "./types.js";
1+
import type { DataCategory, DataSource } from "./types.js";
22
import { SourceFetchError } from "./errors.js";
33
import axios from "axios";
4-
import { BASE_URL } from "../utils/config.js";
4+
import { getBaseUrl } from "../utils/config.js";
55

6-
export async function fetchData(category: DataCategory) {
6+
export async function fetchData(category: DataCategory, source: DataSource = "jsonplaceholder") {
77
try {
8-
const response = await axios.get(`${BASE_URL}/${category}`);
8+
const baseUrl = getBaseUrl(source);
9+
const response = await axios.get(`${baseUrl}/${category}`);
910
return response.data;
1011
} catch (error) {
1112
throw new SourceFetchError(

src/core/generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MockmateOptions } from "./types.js";
1+
import type { DataSource, MockmateOptions } from "./types.js";
22
import { fetchData } from "./fetcher.js";
33
import { generateFromSchema } from "./shema.js";
44

@@ -8,7 +8,7 @@ export async function generate(options: MockmateOptions) {
88
if (options.schema) {
99
result = generateFromSchema(options.schema, options.quantity ?? 1);
1010
} else if (options.category) {
11-
const data = await fetchData(options.category);
11+
const data = await fetchData(options.category, options.source);
1212
result = options.quantity ? data.slice(0, options.quantity) : data;
1313
} else {
1414
throw new Error('Mockmate: provide either "schema" or "category"');

src/core/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export type DataCategory = "users" | "posts" | "comments" | "todos";
22

3+
export type DataSource = "jsonplaceholder" | "fakestoreapi";
4+
35
export type ExtendFn = () => unknown;
46

57
export type GeneratorFn<T = unknown> = () => T;
@@ -9,6 +11,7 @@ export type SchemaDefenititon = Record<string, GeneratorFn>;
911
export interface MockmateOptions {
1012
category: DataCategory;
1113
quantity?: number;
14+
source?: DataSource;
1215
pick?: string[];
1316
extend?: Record<string, ExtendFn>;
1417
schema?: SchemaDefenititon;

src/examples/generate.users.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mockmate } from "../index.js";
33
async function run() {
44
const users = await mockmate({
55
category: "users",
6+
source: "fakestoreapi",
67
quantity: 2,
78
});
89

src/utils/config.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
export const BASE_URL = "https://jsonplaceholder.typicode.com";
2-
// TODO: impleement more data sources. not only jsonplaceholder
3-
// such as https://fakestoreapi.com, and etc.
4-
// and the property source: "jsonplaceholder" | "fakestoreapi" will gitve us that ability.
1+
import type { DataSource } from "../core/types";
2+
3+
export const SOURCE_URLS: Record<DataSource, string> = {
4+
jsonplaceholder: "https://jsonplaceholder.typicode.com",
5+
fakestoreapi: "https://fakestoreapi.com"
6+
};
7+
8+
export const BASE_URL = SOURCE_URLS.jsonplaceholder;
9+
10+
export function getBaseUrl(source: DataSource): string {
11+
return SOURCE_URLS[source];
12+
}

0 commit comments

Comments
 (0)