@auersperg/flare 0.0.0-stage → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ export { z } from "zod";
2
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import type { Validator, Schema } from "./schema.js";
4
+ import type { ReadDatabase, WriteDatabase } from "./database.js";
5
+ export { defineSchema, defineTable, schemaContract } from "./schema.js";
6
+ export type { Validator, Schema, Doc, Value, Input, SchemaContract, } from "./schema.js";
7
+ export type { ReadDatabase, WriteDatabase, Page } from "./database.js";
8
+ import { type Auth, type FunctionKind, type FunctionRef, type Json, type Manifest } from "./protocol.js";
9
+ export interface QueryContext<S extends Schema = Schema> {
10
+ readonly db: ReadDatabase<S>;
11
+ readonly auth: Auth | null;
12
+ }
13
+ export interface MutationContext<S extends Schema = Schema> {
14
+ readonly db: WriteDatabase<S>;
15
+ readonly auth: Auth | null;
16
+ readonly jobs: {
17
+ cancel(id: string): Promise<boolean>;
18
+ schedule(path: string, args: Json, options?: {
19
+ delaySeconds?: number;
20
+ }): Promise<string>;
21
+ };
22
+ }
23
+ export interface ActionContext {
24
+ readonly auth: Auth | null;
25
+ runQuery<I, O>(ref: FunctionRef<I, O>, args: I): Promise<O>;
26
+ runMutation<I, O>(ref: FunctionRef<I, O>, args: I): Promise<O>;
27
+ readonly schedules: {
28
+ set(input: {
29
+ name: string;
30
+ path: string;
31
+ cron: string;
32
+ args: Json;
33
+ }): Promise<{
34
+ id: string;
35
+ }>;
36
+ cancel(name: string): Promise<boolean>;
37
+ list(): Promise<{
38
+ name: string;
39
+ path: string;
40
+ cron: string;
41
+ enabled: boolean;
42
+ }[]>;
43
+ };
44
+ readonly secrets: {
45
+ get(name: string): Promise<string>;
46
+ };
47
+ readonly files: {
48
+ upload(body: ReadableStream<Uint8Array>, contentType: string, options: {
49
+ name: string;
50
+ size: number;
51
+ }): Promise<string>;
52
+ download(id: string): Promise<Response>;
53
+ generateUploadUrl(input: {
54
+ name: string;
55
+ type: string;
56
+ size: number;
57
+ }): Promise<{
58
+ id: string;
59
+ url: string;
60
+ expiresAt: number;
61
+ }>;
62
+ getDownloadUrl(id: string): Promise<{
63
+ url: string;
64
+ expiresAt: number;
65
+ }>;
66
+ delete(id: string): Promise<{
67
+ deleted: boolean;
68
+ }>;
69
+ list(input?: {
70
+ cursor?: string | null;
71
+ limit?: number;
72
+ }): Promise<{
73
+ page: {
74
+ id: string;
75
+ name: string;
76
+ type: string;
77
+ size: number;
78
+ state: string;
79
+ createdAt: number;
80
+ }[];
81
+ continueCursor: string | null;
82
+ isDone: boolean;
83
+ }>;
84
+ };
85
+ }
86
+ type Context<K extends FunctionKind, S extends Schema = Schema> = K extends "action" ? ActionContext : K extends "mutation" ? MutationContext<S> : QueryContext<S>;
87
+ export interface Definition<K extends FunctionKind = FunctionKind, I = any, O = any, S extends Schema = Schema> {
88
+ readonly kind: K;
89
+ readonly input: Validator<any, I>;
90
+ readonly output: Validator<O, any>;
91
+ readonly auth: "required" | "public" | "internal";
92
+ readonly handler: (ctx: Context<K, S>, args: I) => O | Promise<O>;
93
+ }
94
+ export declare const query: <IV extends Validator, OV extends Validator>(options: {
95
+ input: IV;
96
+ output: OV;
97
+ auth?: "required" | "public" | "internal";
98
+ handler: (ctx: QueryContext<Schema>, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
99
+ }) => Definition<"query", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, Schema>;
100
+ export declare const mutation: <IV extends Validator, OV extends Validator>(options: {
101
+ input: IV;
102
+ output: OV;
103
+ auth?: "required" | "public" | "internal";
104
+ handler: (ctx: MutationContext<Schema>, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
105
+ }) => Definition<"mutation", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, Schema>;
106
+ export declare const action: <IV extends Validator, OV extends Validator>(options: {
107
+ input: IV;
108
+ output: OV;
109
+ auth?: "required" | "public" | "internal";
110
+ handler: (ctx: ActionContext, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
111
+ }) => Definition<"action", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, Schema>;
112
+ export declare function createFunctions<S extends Schema>(): {
113
+ query: <IV extends Validator, OV extends Validator>(options: {
114
+ input: IV;
115
+ output: OV;
116
+ auth?: "required" | "public" | "internal";
117
+ handler: (ctx: QueryContext<S>, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
118
+ }) => Definition<"query", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, S>;
119
+ mutation: <IV extends Validator, OV extends Validator>(options: {
120
+ input: IV;
121
+ output: OV;
122
+ auth?: "required" | "public" | "internal";
123
+ handler: (ctx: MutationContext<S>, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
124
+ }) => Definition<"mutation", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, S>;
125
+ action: <IV extends Validator, OV extends Validator>(options: {
126
+ input: IV;
127
+ output: OV;
128
+ auth?: "required" | "public" | "internal";
129
+ handler: (ctx: ActionContext, args: StandardSchemaV1.InferOutput<IV>) => StandardSchemaV1.InferInput<OV> | Promise<StandardSchemaV1.InferInput<OV>>;
130
+ }) => Definition<"action", StandardSchemaV1.InferOutput<IV>, StandardSchemaV1.InferInput<OV>, Schema>;
131
+ };
132
+ export interface FunctionTree {
133
+ [key: string]: Definition<any, any, any, any> | FunctionTree;
134
+ }
135
+ export declare function defineApp<const T extends FunctionTree>(options: {
136
+ functions: T;
137
+ schema: Schema;
138
+ }): Readonly<{
139
+ functions: T;
140
+ schema: Schema;
141
+ }>;
142
+ export declare function flatten(tree: FunctionTree): Record<string, Definition<any, any, any, any>>;
143
+ export declare function manifest(app: {
144
+ functions: FunctionTree;
145
+ }): Manifest;
146
+ export declare function validate<V extends StandardSchemaV1>(validator: V, value: unknown, output?: boolean): Promise<StandardSchemaV1.InferOutput<V>>;
@@ -0,0 +1,78 @@
1
+ export { z } from "zod";
2
+ import { schemaContract } from "./schema.js";
3
+ export { defineSchema, defineTable, schemaContract } from "./schema.js";
4
+ import {
5
+ assertPath,
6
+ boundedJson,
7
+ LIMITS,
8
+ FlareError
9
+ } from "./protocol.js";
10
+ function declare(kind) {
11
+ return (options) => Object.freeze({ ...options, kind, auth: options.auth ?? "required" });
12
+ }
13
+ export const query = declare("query");
14
+ export const mutation = declare("mutation");
15
+ export const action = declare("action");
16
+ export function createFunctions() {
17
+ return {
18
+ query: declare("query"),
19
+ mutation: declare("mutation"),
20
+ action
21
+ };
22
+ }
23
+ export function defineApp(options) {
24
+ flatten(options.functions);
25
+ schemaContract(options.schema);
26
+ return Object.freeze(options);
27
+ }
28
+ export function flatten(tree) {
29
+ const out = Object.create(null);
30
+ const ancestors = new Set;
31
+ function walk(node, prefix, depth) {
32
+ if (depth > 20 || ancestors.has(node))
33
+ throw new FlareError("INVALID_CONTRACT", "Invalid function tree");
34
+ ancestors.add(node);
35
+ for (const [name, value] of Object.entries(node)) {
36
+ const path = prefix ? `${prefix}/${name}` : name;
37
+ assertPath(path);
38
+ if (value && typeof value === "object" && "handler" in value && typeof value.handler === "function") {
39
+ const definition = value;
40
+ if (!["query", "mutation", "action"].includes(definition.kind))
41
+ throw new FlareError("INVALID_CONTRACT", "Invalid function kind");
42
+ if (out[path])
43
+ throw new FlareError("INVALID_CONTRACT", "Duplicate function path");
44
+ out[path] = definition;
45
+ } else if (value && typeof value === "object")
46
+ walk(value, path, depth + 1);
47
+ else
48
+ throw new FlareError("INVALID_CONTRACT", "Invalid function definition");
49
+ }
50
+ ancestors.delete(node);
51
+ }
52
+ walk(tree, "", 0);
53
+ return out;
54
+ }
55
+ export function manifest(app) {
56
+ const functions = Object.create(null);
57
+ for (const [path, fn] of Object.entries(flatten(app.functions))) {
58
+ const input = fn.input["~standard"].jsonSchema?.input({
59
+ target: "draft-2020-12"
60
+ });
61
+ const output = fn.output["~standard"].jsonSchema?.output({
62
+ target: "draft-2020-12"
63
+ });
64
+ if (!input || !output)
65
+ throw new FlareError("INVALID_CONTRACT", `${path} requires JSON Schema converters`);
66
+ functions[path] = { kind: fn.kind, auth: fn.auth, input, output };
67
+ }
68
+ boundedJson(functions, 1048576);
69
+ return { protocol: 1, runtimeAbi: 4, functions };
70
+ }
71
+ export async function validate(validator, value, output = false) {
72
+ boundedJson(value, output ? LIMITS.outputBytes : LIMITS.inputBytes);
73
+ const result = await validator["~standard"].validate(value);
74
+ if (result.issues)
75
+ throw new FlareError(output ? "INVALID_OUTPUT" : "INVALID_INPUT", output ? "Function returned invalid data" : "Arguments failed validation");
76
+ boundedJson(result.value, output ? LIMITS.outputBytes : LIMITS.inputBytes);
77
+ return result.value;
78
+ }
package/package.json CHANGED
@@ -1,6 +1,67 @@
1
1
  {
2
2
  "name": "@auersperg/flare",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.3.2",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "description": "Flare client, server declarations and development CLI",
7
+ "bin": {
8
+ "flare": "./dist/cli/flare.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/sdk/client.d.ts",
18
+ "import": "./dist/sdk/client.js"
19
+ },
20
+ "./server": {
21
+ "types": "./dist/sdk/server.d.ts",
22
+ "import": "./dist/sdk/server.js"
23
+ },
24
+ "./react": {
25
+ "types": "./dist/sdk/react.d.ts",
26
+ "import": "./dist/sdk/react.js"
27
+ },
28
+ "./protocol": {
29
+ "types": "./dist/sdk/protocol.d.ts",
30
+ "import": "./dist/sdk/protocol.js"
31
+ },
32
+ "./cloudflare": {
33
+ "types": "./dist/sdk/cloudflare.d.ts",
34
+ "import": "./dist/sdk/cloudflare.js"
35
+ }
36
+ },
37
+ "dependencies": {
38
+ "@standard-schema/spec": "1.1.0",
39
+ "zod": "4.6.5",
40
+ "json-schema-to-typescript": "16.0.0"
41
+ },
42
+ "engines": {
43
+ "bun": ">=1.4.3"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/auersperg/flare.git",
48
+ "directory": "packages/flare"
49
+ },
50
+ "sideEffects": false,
51
+ "peerDependencies": {
52
+ "react": ">=19"
53
+ },
54
+ "peerDependenciesMeta": {
55
+ "react": {
56
+ "optional": true
57
+ }
58
+ },
59
+ "publishConfig": {
60
+ "access": "public",
61
+ "registry": "https://registry.npmjs.org/",
62
+ "tag": "next"
63
+ },
64
+ "scripts": {
65
+ "prepack": "bun run --cwd ../.. verify"
66
+ }
67
+ }