File tree Expand file tree Collapse file tree 5 files changed +31
-42
lines changed
Expand file tree Collapse file tree 5 files changed +31
-42
lines changed Original file line number Diff line number Diff line change 11import type { DataCategory } from "./types.js" ;
22import { SourceFetchError } from "./errors.js" ;
33import axios from "axios" ;
4-
5- const BASE_URL = "https://jsonplaceholder.typicode.com" ;
4+ import { BASE_URL } from "../utils/config.js" ;
65
76export async function fetchData ( category : DataCategory ) {
87 try {
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 11import 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}
Original file line number Diff line number Diff line change 11import { mockmate } from "../index.js" ;
22
33async 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
1818run ( ) ;
Original file line number Diff line number Diff line change 1+ export const BASE_URL = "https://jsonplaceholder.typicode.com" ;
You can’t perform that action at this time.
0 commit comments