@kube/vgql-core
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -8,2 +8,34 @@ import * as Structype from '@kube/vgql-structype'; | ||
type InputOptionsItem = { | ||
type: "string"; | ||
default?: string; | ||
} | { | ||
type: "number"; | ||
default?: number; | ||
min?: number; | ||
max?: number; | ||
} | { | ||
type: "boolean"; | ||
default?: boolean; | ||
} | { | ||
type: "enum"; | ||
values: string[]; | ||
default?: string; | ||
}; | ||
type InputOptions = { | ||
[key: string]: InputOptionsItem; | ||
}; | ||
type OptionsArgItemFromInputOptionsItem<O extends InputOptionsItem> = O extends { | ||
type: "string"; | ||
} ? string : O extends { | ||
type: "number"; | ||
} ? number : O extends { | ||
type: "boolean"; | ||
} ? boolean : O extends { | ||
type: "enum"; | ||
} ? O["values"][number] : never; | ||
type OptionsArgFromInputOptions<O extends InputOptions> = { | ||
[K in keyof O]: OptionsArgItemFromInputOptionsItem<O[K]>; | ||
}; | ||
type Simplify<T> = { | ||
@@ -28,3 +60,3 @@ [KeyType in keyof T]: T[KeyType]; | ||
}; | ||
type Input<S extends Structype.SchemaDefinition> = { | ||
type Input<S extends Structype.SchemaDefinition, O extends InputOptions> = { | ||
name?: string; | ||
@@ -35,5 +67,7 @@ delay?: { | ||
}; | ||
options?: O; | ||
} & { | ||
factories?: StoreFactories<S>; | ||
store?: StoreInput<S>; | ||
resolvers?: Input_Resolvers<S>; | ||
store?: StoreInput<S, OptionsArgFromInputOptions<O>>; | ||
}; | ||
@@ -56,3 +90,3 @@ | ||
}; | ||
declare function VirtualRef<S extends SchemaDefinition>(_schemaOrBaseState: S | Input<S>): RefCreator<S>; | ||
declare function VirtualRef<S extends SchemaDefinition>(_schemaOrBaseState: S | Input<S, any>): RefCreator<S>; | ||
@@ -68,5 +102,7 @@ type TransformArray<S extends Schema.Array, AllowNested extends boolean = true, ForceTypeName extends boolean = false> = S extends Schema.Array<infer T> ? T extends Schema.Reference ? Array<StoreReference | (AllowNested extends true ? TransformReference<T> : never)> : T extends Schema.Record ? Array<StoreReference | (AllowNested extends true ? RecordToStoreInput<T, AllowNested, ForceTypeName> : never)> : Array<SchemaToType<T>> : never; | ||
}; | ||
type StoreInput<S extends SchemaDefinition> = { | ||
type StoreInput<S extends SchemaDefinition, O> = { | ||
[K in keyof S]?: S[K] extends Schema.Record ? Array<RecordToStoreInput<S[K]>> : never; | ||
}; | ||
} | ((options: O) => { | ||
[K in keyof S]?: S[K] extends Schema.Record ? Array<RecordToStoreInput<S[K]>> : never; | ||
}); | ||
type GetSchemaObjectTypesKeys<S extends SchemaDefinition> = { | ||
@@ -100,3 +136,3 @@ [K in keyof S]: S[K] extends Schema.Record ? K : never; | ||
}; | ||
declare function VirtualServer<S extends SchemaDefinition>(schema: S, temporarySchemaString: string, factories: Factory<S>[]): (inputOrFn: Input<S> | ((Ref: RefCreator<S>) => Input<S>)) => VirtualServer<S>; | ||
declare function VirtualServer<S extends SchemaDefinition, const O extends InputOptions>(schema: S, temporarySchemaString: string, factories: Factory<S>[], options?: OptionsArgFromInputOptions<O>): (inputOrFn: Input<S, O> | ((Ref: RefCreator<S>) => Input<S, O>)) => VirtualServer<S>; | ||
@@ -109,10 +145,10 @@ type VirtualHttpServerHandle<S extends SchemaDefinition> = { | ||
}; | ||
declare function VirtualHttpServer<S extends SchemaDefinition>(Schema: S, rawSchema: string, inputOrFn: Input<S> | ((Ref: RefCreator<S>) => Input<S>), port?: number): Promise<VirtualHttpServerHandle<S>>; | ||
declare function VirtualHttpServer<S extends SchemaDefinition, const O extends InputOptions>(Schema: S, rawSchema: string, inputOrFn: Input<S, O> | ((Ref: RefCreator<S>) => Input<S, O>), port?: number): Promise<VirtualHttpServerHandle<S>>; | ||
type VirtualState<S extends SchemaDefinition> = Input<S>; | ||
declare function VirtualState<S extends SchemaDefinition>(_schema: S): (input: Input<S>) => VirtualState<S>; | ||
type VirtualState<S extends SchemaDefinition, O extends InputOptions> = Input<S, O>; | ||
declare function VirtualState<S extends SchemaDefinition, const O extends InputOptions, const I extends Input<S, O>>(_schema: S): (input: I) => VirtualState<S, O>; | ||
declare namespace VirtualState { | ||
var from: <S extends SchemaDefinition>(baseState: Input<S>) => (state: Input<S>) => Input<S>; | ||
var from: <S extends SchemaDefinition, const O extends InputOptions, const I extends Input<S, O>>(baseState: Input<S, O>) => (state: I) => I; | ||
} | ||
export { VirtualFactory, VirtualHttpServer, VirtualRef, VirtualServer, VirtualState, getStoreReferenceProps }; |
@@ -203,5 +203,27 @@ // src/index.ts | ||
import { | ||
Schema as Schema2, | ||
getSchemaRecordTypes | ||
getSchemaRecordTypes, | ||
Schema as Schema2 | ||
} from "@kube/vgql-structype"; | ||
// src/Options.ts | ||
function defaultOptionArgFromOptionItem(item) { | ||
switch (item.type) { | ||
case "boolean": | ||
return item.default ?? false; | ||
case "number": | ||
return item.default ?? 0; | ||
case "string": | ||
return item.default ?? ""; | ||
case "enum": | ||
return item.default ?? item.values[0]; | ||
} | ||
} | ||
function getDefaultOptionsArgs(optionsSchema) { | ||
return Object.entries(optionsSchema).reduce((acc, [key, item]) => { | ||
acc[key] = defaultOptionArgFromOptionItem(item); | ||
return acc; | ||
}, {}); | ||
} | ||
// src/Store/index.ts | ||
function tranformRecordEntry(_schema, record) { | ||
@@ -216,3 +238,6 @@ return Object.fromEntries( | ||
const objectTypes = getSchemaRecordTypes(schema); | ||
return (input) => { | ||
return (input, options) => { | ||
const defaultOptions = getDefaultOptionsArgs(input.options ?? {}); | ||
const inputOptions = { ...defaultOptions, ...options }; | ||
const inputStore = typeof input.store === "function" ? input.store(inputOptions) : input.store; | ||
const store = Object.fromEntries( | ||
@@ -223,3 +248,3 @@ objectTypes.map( | ||
// @ts-expect-error TOO DEEP: Fix | ||
input.store?.[type.name]?.map( | ||
inputStore?.[type.name]?.map( | ||
(item) => tranformRecordEntry(type, item) | ||
@@ -235,2 +260,3 @@ ) ?? [] | ||
const repository = store[factory.name].map((item, index) => ({ | ||
// @ts-ignore: Too deep | ||
...factory.builder(item, store, index), | ||
@@ -297,7 +323,7 @@ ...item | ||
} | ||
function VirtualServer(schema, temporarySchemaString, factories) { | ||
function VirtualServer(schema, temporarySchemaString, factories, options) { | ||
const Ref = VirtualRef(schema); | ||
return (inputOrFn) => { | ||
const input = typeof inputOrFn === "function" ? inputOrFn(Ref) : inputOrFn; | ||
const store = StoreBuilder(schema, factories)(input ?? {}); | ||
const store = StoreBuilder(schema, factories)(input ?? {}, options); | ||
const resolvers = createDefaultResolvers( | ||
@@ -363,2 +389,10 @@ schema, | ||
VirtualState.from = (baseState) => (state) => { | ||
function applyStore(store, options) { | ||
if (!store) | ||
return {}; | ||
if (typeof store === "function") { | ||
return store(options ?? {}); | ||
} | ||
return store; | ||
} | ||
return { | ||
@@ -370,5 +404,9 @@ ...baseState, | ||
factories: { ...baseState.factories, ...state.factories }, | ||
store: (options) => ({ | ||
// @ts-expect-error | ||
...applyStore(baseState.store, options), | ||
// @ts-expect-error | ||
...applyStore(state.store, options) | ||
}), | ||
// @ts-expect-error | ||
store: { ...baseState.store ?? {}, ...state.store ?? {} }, | ||
// @ts-expect-error | ||
resolvers: deepmerge(baseState.resolvers ?? {}, state.resolvers ?? {}) | ||
@@ -375,0 +413,0 @@ }; |
{ | ||
"name": "@kube/vgql-core", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": "Chris Feijoo", | ||
@@ -18,4 +18,4 @@ "license": "MIT", | ||
"uuid": "^9.0.1", | ||
"@kube/vgql-hkt": "0.0.9", | ||
"@kube/vgql-structype": "0.0.9" | ||
"@kube/vgql-hkt": "0.0.10", | ||
"@kube/vgql-structype": "0.0.10" | ||
}, | ||
@@ -33,2 +33,3 @@ "devDependencies": { | ||
"build": "tsup src/index.ts --format esm --dts --sourcemap", | ||
"dev": "pnpm run build --watch", | ||
"typecheck": "tsc --noEmit", | ||
@@ -35,0 +36,0 @@ "test": "vitest" |
@@ -180,2 +180,4 @@ import { | ||
// TODO: Adapt for Interfaces | ||
const match = context.store[returnTypeName].find( | ||
@@ -182,0 +184,0 @@ (_: any) => _.id === idToFind |
@@ -5,2 +5,3 @@ import { SchemaDefinition } from "@kube/vgql-structype"; | ||
import { Input } from "./Input"; | ||
import { InputOptions } from "./Options"; | ||
import { VirtualServer } from "./Server"; | ||
@@ -22,6 +23,9 @@ import { RefCreator } from "./Store/Reference"; | ||
export async function VirtualHttpServer<S extends SchemaDefinition>( | ||
export async function VirtualHttpServer< | ||
S extends SchemaDefinition, | ||
const O extends InputOptions, | ||
>( | ||
Schema: S, | ||
rawSchema: string, | ||
inputOrFn: Input<S> | ((Ref: RefCreator<S>) => Input<S>), | ||
inputOrFn: Input<S, O> | ((Ref: RefCreator<S>) => Input<S, O>), | ||
port?: number | ||
@@ -32,3 +36,3 @@ ): Promise<VirtualHttpServerHandle<S>> { | ||
const app = express().use(bodyParser.json()); | ||
const virtualServer = VirtualServer(Schema, rawSchema, [])(inputOrFn); | ||
const virtualServer = VirtualServer<S, O>(Schema, rawSchema, [])(inputOrFn); | ||
@@ -35,0 +39,0 @@ app.use(async function (req, res) { |
@@ -6,2 +6,3 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
import { StoreFactories } from "./Factory"; | ||
import { InputOptions, OptionsArgFromInputOptions } from "./Options"; | ||
import { AnythingToStoreInput, Store, StoreInput } from "./Store"; | ||
@@ -24,7 +25,2 @@ | ||
// TODO: Move that to a general helper | ||
// And rename because Unwrap is not the right word | ||
export type UnwrapRef<S extends any> = | ||
S extends Structype.Schema.Reference<() => infer T> ? T : S; | ||
export type ValueOrPromise<T> = T | Promise<T>; | ||
@@ -66,8 +62,13 @@ | ||
export type Input<S extends Structype.SchemaDefinition> = { | ||
export type Input< | ||
S extends Structype.SchemaDefinition, | ||
O extends InputOptions, | ||
> = { | ||
name?: string; | ||
delay?: { global?: number; resolver?: number }; | ||
options?: O; | ||
} & { | ||
factories?: StoreFactories<S>; | ||
store?: StoreInput<S>; | ||
resolvers?: Input_Resolvers<S>; | ||
store?: StoreInput<S, OptionsArgFromInputOptions<O>>; | ||
}; |
@@ -11,2 +11,3 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
import { Input } from "./Input"; | ||
import { InputOptions, OptionsArgFromInputOptions } from "./Options"; | ||
import { GetSchemaObjectTypesKeys, Store, StoreBuilder } from "./Store"; | ||
@@ -35,6 +36,10 @@ import { RefCreator, VirtualRef } from "./Store/Reference"; | ||
// TODO: Remove temporarySchemaString once Serializer from Structype to GraphQL is implemented | ||
export function VirtualServer<S extends SchemaDefinition>( | ||
export function VirtualServer< | ||
S extends SchemaDefinition, | ||
const O extends InputOptions, | ||
>( | ||
schema: S, | ||
temporarySchemaString: string, | ||
factories: Factory<S>[] | ||
factories: Factory<S>[], | ||
options?: OptionsArgFromInputOptions<O> | ||
) { | ||
@@ -44,7 +49,7 @@ const Ref = VirtualRef(schema); | ||
return ( | ||
inputOrFn: Input<S> | ((Ref: RefCreator<S>) => Input<S>) | ||
inputOrFn: Input<S, O> | ((Ref: RefCreator<S>) => Input<S, O>) | ||
): VirtualServer<S> => { | ||
const input = typeof inputOrFn === "function" ? inputOrFn(Ref) : inputOrFn; | ||
const store = StoreBuilder(schema, factories)(input ?? {}); | ||
const store = StoreBuilder<S, O>(schema, factories)(input ?? {}, options); | ||
@@ -51,0 +56,0 @@ const resolvers = createDefaultResolvers( |
@@ -6,12 +6,38 @@ import type { SchemaDefinition } from "@kube/vgql-structype"; | ||
import deepmerge from "deepmerge"; | ||
import { InputOptions, OptionsArgFromInputOptions } from "./Options"; | ||
import { StoreInput } from "./Store"; | ||
export type VirtualState<S extends SchemaDefinition> = Input<S>; | ||
export type VirtualState< | ||
S extends SchemaDefinition, | ||
O extends InputOptions, | ||
> = Input<S, O>; | ||
export function VirtualState<S extends SchemaDefinition>(_schema: S) { | ||
return (input: Input<S>): VirtualState<S> => input; | ||
export function VirtualState< | ||
S extends SchemaDefinition, | ||
const O extends InputOptions, | ||
const I extends Input<S, O>, | ||
>(_schema: S) { | ||
return (input: I): VirtualState<S, O> => input; | ||
} | ||
VirtualState.from = | ||
<S extends SchemaDefinition>(baseState: Input<S>) => | ||
(state: Input<S>): Input<S> => { | ||
< | ||
S extends SchemaDefinition, | ||
const O extends InputOptions, | ||
const I extends Input<S, O>, | ||
>( | ||
baseState: Input<S, O> | ||
) => | ||
(state: I): I => { | ||
function applyStore( | ||
store: StoreInput<S, O> | undefined, | ||
options?: OptionsArgFromInputOptions<O> | ||
) { | ||
if (!store) return {}; | ||
if (typeof store === "function") { | ||
return store(options ?? ({} as any)); | ||
} | ||
return store; | ||
} | ||
return { | ||
@@ -23,7 +49,13 @@ ...baseState, | ||
factories: { ...baseState.factories, ...state.factories }, | ||
store: (options) => ({ | ||
// @ts-expect-error | ||
...applyStore(baseState.store, options), | ||
// @ts-expect-error | ||
...applyStore(state.store, options), | ||
}), | ||
// @ts-expect-error | ||
store: { ...(baseState.store ?? {}), ...(state.store ?? {}) }, | ||
// @ts-expect-error | ||
resolvers: deepmerge(baseState.resolvers ?? {}, state.resolvers ?? {}), | ||
}; | ||
}; |
import { | ||
getSchemaRecordTypes, | ||
Schema, | ||
getSchemaRecordTypes, | ||
type SchemaDefinition, | ||
@@ -10,2 +10,7 @@ type SchemaToType, | ||
import { Input } from "../Input"; | ||
import { | ||
getDefaultOptionsArgs, | ||
InputOptions, | ||
OptionsArgFromInputOptions, | ||
} from "../Options"; | ||
import { uuidTransform } from "../UuidTransform"; | ||
@@ -42,7 +47,2 @@ import type { StoreReference } from "./Reference"; | ||
// TODO: Move that to a general helper | ||
// And rename because Unwrap is not the right word | ||
export type UnwrapRef<S extends any> = | ||
S extends Schema.Reference<() => infer T> ? T : S; | ||
type UnionToStoreInput< | ||
@@ -57,3 +57,2 @@ S extends Schema.Union, | ||
: never; | ||
// | (S extends Schema.Union<infer XS> ? SchemaToType<UnwrapRef<XS>> : S); | ||
@@ -96,7 +95,13 @@ // TODO: Rename that | ||
export type StoreInput<S extends SchemaDefinition> = { | ||
[K in keyof S]?: S[K] extends Schema.Record | ||
? Array<RecordToStoreInput<S[K]>> | ||
: never; | ||
}; | ||
export type StoreInput<S extends SchemaDefinition, O> = | ||
| { | ||
[K in keyof S]?: S[K] extends Schema.Record | ||
? Array<RecordToStoreInput<S[K]>> | ||
: never; | ||
} | ||
| ((options: O) => { | ||
[K in keyof S]?: S[K] extends Schema.Record | ||
? Array<RecordToStoreInput<S[K]>> | ||
: never; | ||
}); | ||
@@ -113,3 +118,6 @@ export type GetSchemaObjectTypesKeys<S extends SchemaDefinition> = { | ||
type StoreCreator<S extends SchemaDefinition> = (input: Input<S>) => Store<S>; | ||
type StoreCreator<S extends SchemaDefinition, O extends InputOptions> = ( | ||
input: Input<S, O>, | ||
options?: OptionsArgFromInputOptions<O> | ||
) => Store<S>; | ||
@@ -152,10 +160,20 @@ /** | ||
export function StoreBuilder<S extends SchemaDefinition>( | ||
schema: S, | ||
factories: Factory<S>[] = [] | ||
): StoreCreator<S> { | ||
export function StoreBuilder< | ||
S extends SchemaDefinition, | ||
const O extends InputOptions, | ||
>(schema: S, factories: Factory<S>[] = []): StoreCreator<S, O> { | ||
const objectTypes = getSchemaRecordTypes(schema); | ||
return (input) => { | ||
return (input, options) => { | ||
// | ||
// Prepare: Build default options, and apply them to input.store if is function | ||
// | ||
const defaultOptions = getDefaultOptionsArgs(input.options ?? ({} as any)); | ||
const inputOptions = { ...defaultOptions, ...options } as any; | ||
const inputStore = | ||
typeof input.store === "function" | ||
? input.store(inputOptions) | ||
: input.store; | ||
// | ||
// First phase: Only split input in partial Arrays (for each Repository) | ||
@@ -171,3 +189,3 @@ // Create References to other entries in Store | ||
// @ts-expect-error TOO DEEP: Fix | ||
input.store?.[type.name!]?.map((item) => | ||
inputStore?.[type.name!]?.map((item) => | ||
tranformRecordEntry(type, item) | ||
@@ -192,2 +210,3 @@ ) ?? [], | ||
const repository = store[factory.name].map((item, index) => ({ | ||
// @ts-ignore: Too deep | ||
...factory.builder(item as any, store, index), | ||
@@ -194,0 +213,0 @@ ...item, |
@@ -43,3 +43,3 @@ import { SchemaDefinition } from "@kube/vgql-structype"; | ||
export function VirtualRef<S extends SchemaDefinition>( | ||
_schemaOrBaseState: S | Input<S> | ||
_schemaOrBaseState: S | Input<S, any> | ||
): RefCreator<S> { | ||
@@ -46,0 +46,0 @@ return new Proxy( |
Sorry, the diff of this file is not supported yet
98283
24
1803
+ Added@kube/vgql-hkt@0.0.10(transitive)
+ Added@kube/vgql-structype@0.0.10(transitive)
- Removed@kube/vgql-hkt@0.0.9(transitive)
- Removed@kube/vgql-structype@0.0.9(transitive)
Updated@kube/vgql-hkt@0.0.10
Updated@kube/vgql-structype@0.0.10