File tree Expand file tree Collapse file tree 4 files changed +51
-4
lines changed
Expand file tree Collapse file tree 4 files changed +51
-4
lines changed Original file line number Diff line number Diff line change 11import type { MockmateOptions } from "./types.js" ;
22import { fetchData } from "./fetcher.js" ;
3+ import { generateFromSchema } from "./shema.js" ;
34
45export async function generate ( options : MockmateOptions ) {
5- const data = await fetchData ( options . category ) ;
6+ let result : any [ ] = [ ] ;
67
7- let result = options . quantity ? data . slice ( 0 , options . quantity ) : data ;
8+ if ( options . schema ) {
9+ result = generateFromSchema (
10+ options . schema ,
11+ options . quantity ?? 1
12+ ) ;
13+ }
14+
15+ else if ( options . category ) {
16+ 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+ ) ;
26+ }
827
928 if ( options . pick ) {
1029 result = result . map ( ( item : Record < string , unknown > ) => {
@@ -22,7 +41,10 @@ export async function generate(options: MockmateOptions) {
2241 result = result . map ( ( item : Record < string , unknown > ) => ( {
2342 ...item ,
2443 ...Object . fromEntries (
25- Object . entries ( options . extend ! ) . map ( ( [ key , fn ] ) => [ key , fn ( ) ] )
44+ Object . entries ( options . extend ! ) . map ( ( [ key , fn ] ) => [
45+ key ,
46+ fn ( ) ,
47+ ] )
2648 ) ,
2749 } ) ) ;
2850 }
Original file line number Diff line number Diff line change 1+ import type { SchemaDefenititon } from "./types" ;
2+
3+ export function generateFromSchema ( schema : SchemaDefenititon , quantity : number = 1 ) {
4+ const result = [ ] ;
5+
6+ for ( let i = 0 ; i < quantity ; i ++ ) {
7+ const item : Record < string , unknown > = { } ;
8+
9+ for ( const key in schema ) {
10+ item [ key ] = schema [ key ] ( ) ;
11+ }
12+
13+ result . push ( item )
14+ }
15+
16+ return result ;
17+ }
Original file line number Diff line number Diff line change @@ -2,9 +2,14 @@ export type DataCategory = "users" | "posts" | "comments" | "todos";
22
33export type ExtendFn = ( ) => unknown ;
44
5+ export type GeneratorFn < T = unknown > = ( ) => T ;
6+
7+ export type SchemaDefenititon = Record < string , GeneratorFn > ;
8+
59export interface MockmateOptions {
610 category : DataCategory ;
711 quantity ?: number ;
812 pick ?: string [ ] ;
913 extend ?: Record < string , ExtendFn > ;
14+ schema ?: SchemaDefenititon ;
1015}
Original file line number Diff line number Diff line change @@ -2,8 +2,11 @@ import { mockmate } from "../index.js";
22
33async function run ( ) {
44 const users = await mockmate ( {
5- category : "posts " ,
5+ category : "users " ,
66 quantity : 2 ,
7+ extend : {
8+ zipcode : ( ) => "90566 - 7771"
9+ } ,
710 } ) ;
811
912 console . groupCollapsed ( "Generated users: " ) ;
You can’t perform that action at this time.
0 commit comments