cloesce 0.0.5-unstable.4 → 0.0.5-unstable.6

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.
@@ -1,103 +0,0 @@
1
- import { MediaType } from "../ast.js";
2
- type DeepPartialInner<T> = T extends (infer U)[] ? DeepPartialInner<U>[] : T extends object ? {
3
- [K in keyof T]?: DeepPartialInner<T[K]>;
4
- } : T | (null extends T ? null : never);
5
- /**
6
- * Recursively makes all properties of a type optional — including nested objects and arrays.
7
- *
8
- * Similar to TypeScript's built-in `Partial<T>`, but applies the transformation deeply across
9
- * all nested structures. Useful for defining "patch" or "update" objects where only a subset
10
- * of properties may be provided.
11
- *
12
- * **Apart of the Cloesce method grammar**, meaning the type can be apart of method parameters
13
- * or return types and the generated workers and client API will act accordingly.
14
- *
15
- * @template T
16
- * The target type to make deeply partial.
17
- *
18
- * @remarks
19
- * - **Objects:** All properties become optional, and their values are recursively wrapped in `DeepPartial`.
20
- * - **Arrays:** Arrays are preserved, but their elements are recursively made partial.
21
- * - **Scalars:** Primitive values (string, number, boolean, etc.) remain unchanged.
22
- * - **Nullable types:** If `null` is assignable to the type, it remains allowed.
23
- *
24
- * @example
25
- * ```ts
26
- * class User {
27
- * id: string;
28
- * profile: {
29
- * name: string;
30
- * age: number;
31
- * };
32
- * tags: string[];
33
- * }
34
- *
35
- * // The resulting type:
36
- * // {
37
- * // id?: string;
38
- * // profile?: { name?: string; age?: number };
39
- * // tags?: (string | undefined)[];
40
- * // }
41
- * type PartialUser = DeepPartial<User>;
42
- *
43
- * const patch: PartialUser = {
44
- * profile: { age: 30 } // ok
45
- * };
46
- * ```
47
- */
48
- export type DeepPartial<T> = DeepPartialInner<T> & {
49
- __brand?: "Partial";
50
- };
51
- export declare class Either<L, R> {
52
- private readonly inner;
53
- private constructor();
54
- get value(): L | R;
55
- static left<R>(): Either<void, R>;
56
- static left<L, R = never>(value: L): Either<L, R>;
57
- static right<R, L = never>(value: R): Either<L, R>;
58
- isLeft(): this is Either<L, never>;
59
- isRight(): this is Either<never, R>;
60
- unwrap(): R;
61
- unwrapLeft(): L;
62
- map<B>(fn: (val: R) => B): Either<L, B>;
63
- mapLeft<B>(fn: (val: L) => B): Either<B, R>;
64
- }
65
- /**
66
- * Given a media type and some data, converts to a proper
67
- * `RequestInit` body,
68
- */
69
- export declare function requestBody(mediaType: MediaType, data: any | string | undefined): undefined | string | ReadableStream<Uint8Array>;
70
- /**
71
- * The result of a Workers endpoint.
72
- *
73
- * @param ok True if `status` < 400
74
- * @param status The HTTP Status of a Workers request
75
- * @param headers All headers that the result is to be sent with or was received with
76
- * @param data JSON data yielded from a request, undefined if the request was not `ok`.
77
- * @param message An error text set if the request was not `ok`.
78
- *
79
- * @remarks If `status` is 204 `data` will always be undefined.
80
- *
81
- */
82
- export declare class HttpResult<T = unknown> {
83
- ok: boolean;
84
- status: number;
85
- headers: Headers;
86
- data?: T | undefined;
87
- message?: string | undefined;
88
- mediaType?: MediaType | undefined;
89
- constructor(ok: boolean, status: number, headers: Headers, data?: T | undefined, message?: string | undefined, mediaType?: MediaType | undefined);
90
- static ok<T>(status: number, data?: T, init?: HeadersInit): HttpResult;
91
- static fail(status: number, message?: string, init?: HeadersInit): HttpResult<never>;
92
- toResponse(): Response;
93
- setMediaType(mediaType: MediaType): this;
94
- static fromResponse(response: Response, mediaType: MediaType, ctor?: any, array?: boolean): Promise<HttpResult<any>>;
95
- }
96
- export type Stream = ReadableStream<Uint8Array>;
97
- export declare function b64ToU8(b64: string): Uint8Array;
98
- export declare function u8ToB64(u8: Uint8Array): string;
99
- export type KeysOfType<T, U> = {
100
- [K in keyof T]: T[K] extends U ? (K extends string ? K : never) : never;
101
- }[keyof T];
102
- export {};
103
- //# sourceMappingURL=common.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/ui/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAA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CAAC,CAAC;CAwD5B;AAED,MAAM,MAAM,MAAM,GAAG,cAAc,CAAC,UAAU,CAAC,CAAC;AAEhD,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAc/C;AAED,wBAAgB,OAAO,CAAC,EAAE,EAAE,UAAU,GAAG,MAAM,CAY9C;AAED,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,IAAI;KAC5B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK;CACxE,CAAC,MAAM,CAAC,CAAC,CAAC"}
package/dist/ui/common.js DELETED
@@ -1,191 +0,0 @@
1
- import { MediaType } from "../ast.js";
2
- export class Either {
3
- inner;
4
- constructor(inner) {
5
- this.inner = inner;
6
- }
7
- get value() {
8
- return this.inner.ok ? this.inner.right : this.inner.left;
9
- }
10
- static left(value) {
11
- return new Either({ ok: false, left: value });
12
- }
13
- static right(value) {
14
- return new Either({ ok: true, right: value });
15
- }
16
- isLeft() {
17
- return !this.inner.ok;
18
- }
19
- isRight() {
20
- return this.inner.ok;
21
- }
22
- unwrap() {
23
- if (!this.inner.ok) {
24
- throw new Error("Tried to unwrap a Left value");
25
- }
26
- return this.inner.right;
27
- }
28
- unwrapLeft() {
29
- if (this.inner.ok) {
30
- throw new Error("Tried to unwrapLeft a Right value");
31
- }
32
- return this.inner.left;
33
- }
34
- map(fn) {
35
- return this.inner.ok
36
- ? Either.right(fn(this.inner.right))
37
- : Either.left(this.inner.left);
38
- }
39
- mapLeft(fn) {
40
- return this.inner.ok
41
- ? Either.right(this.inner.right)
42
- : Either.left(fn(this.inner.left));
43
- }
44
- }
45
- /**
46
- * Given a media type and some data, converts to a proper
47
- * `RequestInit` body,
48
- */
49
- export function requestBody(mediaType, data) {
50
- switch (mediaType) {
51
- case MediaType.Json: {
52
- return JSON.stringify(data ?? {}, (_, v) => v instanceof Uint8Array ? u8ToB64(v) : v);
53
- }
54
- case MediaType.Octet: {
55
- // JSON structure isn't needed; assume the first
56
- // value is the stream data
57
- return Object.values(data)[0];
58
- }
59
- }
60
- }
61
- /**
62
- * The result of a Workers endpoint.
63
- *
64
- * @param ok True if `status` < 400
65
- * @param status The HTTP Status of a Workers request
66
- * @param headers All headers that the result is to be sent with or was received with
67
- * @param data JSON data yielded from a request, undefined if the request was not `ok`.
68
- * @param message An error text set if the request was not `ok`.
69
- *
70
- * @remarks If `status` is 204 `data` will always be undefined.
71
- *
72
- */
73
- export class HttpResult {
74
- ok;
75
- status;
76
- headers;
77
- data;
78
- message;
79
- mediaType;
80
- constructor(ok, status, headers, data, message, mediaType) {
81
- this.ok = ok;
82
- this.status = status;
83
- this.headers = headers;
84
- this.data = data;
85
- this.message = message;
86
- this.mediaType = mediaType;
87
- }
88
- static ok(status, data, init) {
89
- const headers = new Headers(init);
90
- return new HttpResult(true, status, headers, data, undefined);
91
- }
92
- static fail(status, message, init) {
93
- const headers = new Headers(init);
94
- return new HttpResult(false, status, headers, undefined, message);
95
- }
96
- toResponse() {
97
- switch (this.mediaType) {
98
- case MediaType.Json: {
99
- this.headers.set("Content-Type", "application/json");
100
- break;
101
- }
102
- case MediaType.Octet: {
103
- this.headers.set("Content-Type", "application/octet-stream");
104
- break;
105
- }
106
- case undefined: {
107
- // Errors are always text.
108
- this.headers.set("Content-Type", "text/plain");
109
- return new Response(this.message, {
110
- status: this.status,
111
- headers: this.headers,
112
- });
113
- }
114
- }
115
- return new Response(requestBody(this.mediaType, this.data), {
116
- status: this.status,
117
- headers: this.headers,
118
- });
119
- }
120
- setMediaType(mediaType) {
121
- this.mediaType = mediaType;
122
- return this;
123
- }
124
- static async fromResponse(response, mediaType, ctor, array = false) {
125
- if (response.status >= 400) {
126
- return new HttpResult(false, response.status, response.headers, undefined, await response.text());
127
- }
128
- function instantiate(json, ctor) {
129
- switch (ctor) {
130
- case Date: {
131
- return new Date(json);
132
- }
133
- case Uint8Array: {
134
- return b64ToU8(json);
135
- }
136
- case undefined: {
137
- return json;
138
- }
139
- default: {
140
- return ctor.fromJson(json);
141
- }
142
- }
143
- }
144
- async function data() {
145
- switch (mediaType) {
146
- case MediaType.Json: {
147
- let json = await response.json();
148
- if (array) {
149
- for (let i = 0; i < json.length; i++) {
150
- json[i] = instantiate(json[i], ctor);
151
- }
152
- }
153
- else {
154
- json = instantiate(json, ctor);
155
- }
156
- return json;
157
- }
158
- case MediaType.Octet: {
159
- return response.body;
160
- }
161
- }
162
- }
163
- return new HttpResult(true, response.status, response.headers, await data());
164
- }
165
- }
166
- export function b64ToU8(b64) {
167
- // Prefer Buffer in Node.js environments
168
- if (typeof Buffer !== "undefined") {
169
- const buffer = Buffer.from(b64, "base64");
170
- return new Uint8Array(buffer);
171
- }
172
- // Use atob only in browser environments
173
- const s = atob(b64);
174
- const u8 = new Uint8Array(s.length);
175
- for (let i = 0; i < s.length; i++) {
176
- u8[i] = s.charCodeAt(i);
177
- }
178
- return u8;
179
- }
180
- export function u8ToB64(u8) {
181
- // Prefer Buffer in Node.js environments
182
- if (typeof Buffer !== "undefined") {
183
- return Buffer.from(u8).toString("base64");
184
- }
185
- // Use btoa only in browser environments
186
- let s = "";
187
- for (let i = 0; i < u8.length; i++) {
188
- s += String.fromCharCode(u8[i]);
189
- }
190
- return btoa(s);
191
- }