Skip to content

Commit

Permalink
TypeScript client SDK for apply
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro committed Feb 14, 2024
1 parent a798ff1 commit b3eb807
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 51 deletions.
15 changes: 6 additions & 9 deletions typegraph/node/sdk/src/runtimes/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ export class DenoRuntime extends Runtime {
}

func<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
{ code, secrets = [], effect = fx.read() }: DenoFunc,
): t.Func<P, I, O, FunMat> {
): t.Func<I, O, FunMat> {
const matId = runtimes.registerDenoFunc({ code, secrets }, effect);
const mat: FunMat = {
_id: matId,
Expand All @@ -63,14 +62,13 @@ export class DenoRuntime extends Runtime {
}

import<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
{ name, module, effect = fx.read(), secrets = [] }: DenoImport,
): t.Func<P, I, O, ImportMat> {
): t.Func<I, O, ImportMat> {
const matId = runtimes.importDenoFunction({
funcName: name,
module,
Expand All @@ -87,9 +85,8 @@ export class DenoRuntime extends Runtime {
}

identity<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
>(inp: I): t.Func<P, I, I, PredefinedFuncMat> {
I extends t.Typedef = t.Typedef,
>(inp: I): t.Func<I, I, PredefinedFuncMat> {
const mat: PredefinedFuncMat = {
_id: runtimes.getPredefinedDenoFunc({ name: "identity" }),
name: "identity",
Expand Down
10 changes: 4 additions & 6 deletions typegraph/node/sdk/src/runtimes/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ export class GraphQLRuntime extends Runtime {
}

query<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(inp: I, out: O, path?: string[]): t.Func<P, I, O, QueryMat> {
>(inp: I, out: O, path?: string[]): t.Func<I, O, QueryMat> {
const matId = runtimes.graphqlQuery({
runtime: this._id,
effect: fx.read(),
Expand All @@ -33,15 +32,14 @@ export class GraphQLRuntime extends Runtime {
}

mutation<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
effect: Effect,
path?: string[],
): t.Func<P, I, O, MutationMat> {
): t.Func<I, O, MutationMat> {
const matId = runtimes.graphqlMutation({
runtime: this._id,
effect,
Expand Down
30 changes: 12 additions & 18 deletions typegraph/node/sdk/src/runtimes/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ export class HttpRuntime extends Runtime {

#request<
M extends HttpMethod,
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
method: M,
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
effect: Effect,
): t.Func<P, I, O> {
): t.Func<I, O> {
const matId = runtimes.httpRequest({
runtime: this._id,
effect,
Expand All @@ -57,66 +56,61 @@ export class HttpRuntime extends Runtime {
}

get<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
): t.Func<P, I, O> {
): t.Func<I, O> {
return this.#request("get", inp, out, options, fx.read());
}

post<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
effect?: Effect,
): t.Func<P, I, O> {
): t.Func<I, O> {
return this.#request("get", inp, out, options, effect ?? fx.create());
}

put<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
effect?: Effect,
): t.Func<P, I, O> {
): t.Func<I, O> {
return this.#request("get", inp, out, options, effect ?? fx.update());
}

patch<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
effect?: Effect,
): t.Func<P, I, O> {
): t.Func<I, O> {
return this.#request("patch", inp, out, options, effect ?? fx.update());
}

delete_<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
options: Omit<MaterializerHttpRequest, "method">,
effect?: Effect,
): t.Func<P, I, O> {
): t.Func<I, O> {
return this.#request("get", inp, out, options, effect ?? fx.delete_());
}
}
5 changes: 2 additions & 3 deletions typegraph/node/sdk/src/runtimes/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ export class PythonRuntime extends Runtime {
}

import<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
out: O,
{ name, module, effect = fx.read(), secrets = [] }: PythonImport,
): t.Func<P, I, O, ImportMat> {
): t.Func<I, O, ImportMat> {
const base = {
runtime: this._id,
effect,
Expand Down
5 changes: 2 additions & 3 deletions typegraph/node/sdk/src/runtimes/wasmedge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export class WasmEdgeRuntime extends Runtime {
}

wasi<
P extends Record<string, t.Typedef> = Record<string, t.Typedef>,
I extends t.Struct<P> = t.Struct<P>,
I extends t.Typedef = t.Typedef,
O extends t.Typedef = t.Typedef,
>(
inp: I,
Expand All @@ -30,7 +29,7 @@ export class WasmEdgeRuntime extends Runtime {
wasm: string;
effect?: Effect;
},
): t.Func<P, I, O, WasiMat> {
): t.Func<I, O, WasiMat> {
const matId = runtimes.fromWasiModule(
{
runtime: this._id,
Expand Down
35 changes: 33 additions & 2 deletions typegraph/node/sdk/src/typegraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,37 @@ interface TypegraphArgs {
disableAutoSerialization?: boolean;
}

export interface TypegraphBuilderArgs {
export class ApplyFromArg {
constructor(public name: string | null) { }
}

export class ApplyFromStatic {
constructor(public value: any) { }
}

export class ApplyFromSecret {
constructor(public key: string) { }
}

export class ApplyFromContext {
constructor(public key: string) { }
}

export class ApplyFromParent {
constructor(public typeName: string) { }
}

const InjectionSource = {
asArg: (name?: string) => new ApplyFromArg(name ?? null),
set: (value: any) => new ApplyFromStatic(value),
fromSecret: (key: string) => new ApplyFromSecret(key),
fromContext: (key: string) => new ApplyFromContext(key),
fromParent: (typeName: string) => new ApplyFromParent(typeName),
} as const;

type InjectionSourceType = typeof InjectionSource;

export interface TypegraphBuilderArgs extends InjectionSourceType {
expose: (exports: Exports, defaultPolicy?: Policy) => void;
inherit: () => InheritDef;
rest: (graphql: string) => number;
Expand Down Expand Up @@ -68,7 +98,7 @@ export class InheritDef {
export type TypegraphBuilder = (g: TypegraphBuilderArgs) => void;

export class RawAuth {
constructor(readonly jsonStr: string) {}
constructor(readonly jsonStr: string) { }
}

export interface TypegraphOutput {
Expand Down Expand Up @@ -164,6 +194,7 @@ export function typegraph(
ref: (name: string) => {
return genRef(name);
},
...InjectionSource,
};

builder(g);
Expand Down
Loading

0 comments on commit b3eb807

Please sign in to comment.