@yaks/native 0.0.0-stage → 0.2.36
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.
- package/LICENSE +202 -0
- package/README.md +65 -2
- package/_dist/look.d.ts +15 -0
- package/_dist/look.d.ts.map +1 -0
- package/_dist/mod.d.ts +130 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/remote.d.ts +45 -0
- package/_dist/remote.d.ts.map +1 -0
- package/deno.json +1 -0
- package/look.js +171 -0
- package/look.js.map +1 -0
- package/look.ts +81 -0
- package/mod.js +375 -0
- package/mod.js.map +1 -0
- package/mod.ts +555 -0
- package/package.json +25 -4
- package/remote.js +164 -0
- package/remote.js.map +1 -0
- package/remote.ts +222 -0
package/remote.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A kit-built app's view, loaded from the app itself, and its store over
|
|
3
|
+
* HTTP: what any host that draws the view outside a browser needs of the app.
|
|
4
|
+
* The iOS shell and `yak open` in a terminal are two such hosts; each hands
|
|
5
|
+
* in its own `fetch` (the phone's signed-in cookie, the CLI's token) and the
|
|
6
|
+
* packages it carries.
|
|
7
|
+
*
|
|
8
|
+
* An app names its view in its front page,
|
|
9
|
+
* `<link rel="yaks-native" href="view.ts">`. `viewAt` fetches that module and
|
|
10
|
+
* what it imports by relative path, compiles each from TypeScript, and runs
|
|
11
|
+
* it with the host's own copies of the app's `"platform"` packages standing
|
|
12
|
+
* in for them, as the platform's compile supplies them to its page; the
|
|
13
|
+
* host's copies are the ones its painter's DOM is rendered by. The module's
|
|
14
|
+
* default export is a component that takes the app's `store`.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/ import { transform } from "sucrase";
|
|
18
|
+
/** The app's view could not be loaded; its web page still opens. */ export class Unloadable extends Error {
|
|
19
|
+
}
|
|
20
|
+
/** A path against the URL it is named from, as a browser resolves an href:
|
|
21
|
+
* absolute, from the origin, or beside the file.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { assertEquals } from '@std/assert'
|
|
25
|
+
* let page = 'https://a.yaks.app/packing/'
|
|
26
|
+
* assertEquals(against(page, 'view.ts'), 'https://a.yaks.app/packing/view.ts')
|
|
27
|
+
* assertEquals(against(page + 'lib/a.ts', '../b.ts'), page + 'b.ts')
|
|
28
|
+
* assertEquals(against(page, '/x/'), 'https://a.yaks.app/x/')
|
|
29
|
+
* ```
|
|
30
|
+
*/ export let against = (from, path)=>{
|
|
31
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return path;
|
|
32
|
+
let origin = from.match(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i)?.[0] ?? '';
|
|
33
|
+
if (path.startsWith('/')) return origin + path;
|
|
34
|
+
let parts = from.split('/').slice(0, -1);
|
|
35
|
+
for (let p of path.split('/')){
|
|
36
|
+
if (p == '..') parts.length > 3 && parts.pop();
|
|
37
|
+
else if (p != '.') parts.push(p);
|
|
38
|
+
}
|
|
39
|
+
return parts.join('/');
|
|
40
|
+
};
|
|
41
|
+
let tag = (html, name, rel)=>[
|
|
42
|
+
...html.matchAll(new RegExp(`<${name}\\b[^>]*>`, 'gi'))
|
|
43
|
+
].map((m)=>m[0]).find((t)=>!rel || new RegExp(`\\brel=["']?${rel}["'\\s>]`, 'i').test(t));
|
|
44
|
+
let href = (t)=>t && /\bhref=["']?([^"'\s>]+)/i.exec(t)?.[1];
|
|
45
|
+
/** The URL of the view a page names, against its `<base>` when it has one,
|
|
46
|
+
* or null when it names none.
|
|
47
|
+
*
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { assertEquals } from '@std/assert'
|
|
50
|
+
* let html = '<base href="/packing/"><link rel="yaks-native" href="view.ts">'
|
|
51
|
+
* assertEquals(
|
|
52
|
+
* viewOf(html, 'https://a.yaks.app/packing'),
|
|
53
|
+
* 'https://a.yaks.app/packing/view.ts',
|
|
54
|
+
* )
|
|
55
|
+
* assertEquals(viewOf('<link rel="icon" href="i.png">', 'https://a/'), null)
|
|
56
|
+
* ```
|
|
57
|
+
*/ export let viewOf = (html, page)=>{
|
|
58
|
+
let view = href(tag(html, 'link', 'yaks-native'));
|
|
59
|
+
if (!view) return null;
|
|
60
|
+
let base = href(tag(html, 'base'));
|
|
61
|
+
return against(base ? against(page, base) : page, view);
|
|
62
|
+
};
|
|
63
|
+
/** The URL of the view an app's page names, or null when it names none. */ export let nativeOf = async (page, go = fetch)=>{
|
|
64
|
+
let r = await go(page);
|
|
65
|
+
if (!r.ok) {
|
|
66
|
+
await r.body?.cancel();
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return viewOf(await r.text(), r.url || page);
|
|
70
|
+
};
|
|
71
|
+
let gather = async (url, go, sources)=>{
|
|
72
|
+
if (sources.has(url)) return;
|
|
73
|
+
sources.set(url, undefined);
|
|
74
|
+
let r = await go(url);
|
|
75
|
+
if (!r.ok) {
|
|
76
|
+
await r.body?.cancel();
|
|
77
|
+
throw new Unloadable(`${url} answered ${r.status}`);
|
|
78
|
+
}
|
|
79
|
+
let { code } = transform(await r.text(), {
|
|
80
|
+
transforms: [
|
|
81
|
+
'typescript',
|
|
82
|
+
'imports'
|
|
83
|
+
],
|
|
84
|
+
filePath: url
|
|
85
|
+
});
|
|
86
|
+
let names = [
|
|
87
|
+
...code.matchAll(/require\(['"]([^'"]+)['"]\)/g)
|
|
88
|
+
].map((m)=>m[1]);
|
|
89
|
+
sources.set(url, {
|
|
90
|
+
code,
|
|
91
|
+
names
|
|
92
|
+
});
|
|
93
|
+
for (let name of names){
|
|
94
|
+
if (name.startsWith('.')) await gather(against(url, name), go, sources);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
let execute = (url, carried, sources, modules)=>{
|
|
98
|
+
let had = modules.get(url);
|
|
99
|
+
if (had) return had;
|
|
100
|
+
let module = {
|
|
101
|
+
exports: {}
|
|
102
|
+
};
|
|
103
|
+
modules.set(url, module);
|
|
104
|
+
let need = (name)=>name.startsWith('.') ? execute(against(url, name), carried, sources, modules).exports : carried[name];
|
|
105
|
+
new Function('require', 'module', 'exports', sources.get(url).code)(need, module, module.exports);
|
|
106
|
+
return module;
|
|
107
|
+
};
|
|
108
|
+
/** Fetch a view's module graph so its host can select the renderer before it runs. */ export let prepareView = async (url, go = fetch)=>{
|
|
109
|
+
let sources = new Map();
|
|
110
|
+
await gather(url, go, sources);
|
|
111
|
+
let names = [
|
|
112
|
+
...new Set([
|
|
113
|
+
...sources.values()
|
|
114
|
+
].flatMap((s)=>s.names.filter((name)=>!name.startsWith('.'))))
|
|
115
|
+
];
|
|
116
|
+
return {
|
|
117
|
+
imports: names,
|
|
118
|
+
open (carried) {
|
|
119
|
+
for (let name of names){
|
|
120
|
+
if (!(name in carried)) {
|
|
121
|
+
throw new Unloadable(`it imports ${name}, which this host does not carry`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
let view = execute(url, carried, sources, new Map()).exports.default;
|
|
125
|
+
if (typeof view != 'function') {
|
|
126
|
+
throw new Unloadable(`${url} exports no default view`);
|
|
127
|
+
}
|
|
128
|
+
return view;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
/** Load a view module and what it imports, its platform packages the host's. */ export let viewAt = async (url, carried, go = fetch)=>(await prepareView(url, go)).open(carried);
|
|
133
|
+
// What a refusal says: the store's own words, else its status.
|
|
134
|
+
let said = async (r, doing)=>{
|
|
135
|
+
let body = await r.text();
|
|
136
|
+
let message;
|
|
137
|
+
try {
|
|
138
|
+
let json = JSON.parse(body);
|
|
139
|
+
message = json.message ?? json.error?.message ?? json.error;
|
|
140
|
+
} catch {}
|
|
141
|
+
return new Error(typeof message == 'string' ? message : `${doing} (HTTP ${r.status})`);
|
|
142
|
+
};
|
|
143
|
+
/** An app's store over HTTP, at its `api` (`https://a.yaks.app/packing/api`),
|
|
144
|
+
* as its page's `./api/client.js` offers it. */ export let store = (api, go = fetch)=>({
|
|
145
|
+
query: async (filter)=>{
|
|
146
|
+
let r = await go(`${api}/query?${filter}`);
|
|
147
|
+
if (!r.ok) throw await said(r, 'Could not read this app');
|
|
148
|
+
return await r.json();
|
|
149
|
+
},
|
|
150
|
+
apply: async (bundles)=>{
|
|
151
|
+
let r = await go(`${api}/apply`, {
|
|
152
|
+
method: 'POST',
|
|
153
|
+
headers: {
|
|
154
|
+
'content-type': 'application/json'
|
|
155
|
+
},
|
|
156
|
+
body: JSON.stringify({
|
|
157
|
+
entities: bundles
|
|
158
|
+
})
|
|
159
|
+
});
|
|
160
|
+
if (!r.ok) throw await said(r, 'Could not save in this app');
|
|
161
|
+
return await r.json();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
//# sourceMappingURL=remote.js.map
|
package/remote.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote.js","sources":["./remote.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;CAgBC,GAED,SAAS,SAAS,kBAA4B;AAgB9C,kEAAkE,GAClE,OAAO,MAAM,mBAAmB;AAAO;AAEvC;;;;;;;;;;CAUC,GACD,OAAO,IAAI,UAAU,CAAC,MAAc;EAClC,IAAI,uBAAuB,IAAI,CAAC,OAAO,OAAO;EAC9C,IAAI,SAAS,KAAK,KAAK,CAAC,kCAAkC,CAAC,EAAE,IAAI;EACjE,IAAI,KAAK,UAAU,CAAC,MAAM,OAAO,SAAS;EAC1C,IAAI,QAAQ,KAAK,KAAK,CAAC,KAAK,KAAK,CAAC,GAAG,CAAC;EACtC,KAAK,IAAI,KAAK,KAAK,KAAK,CAAC,KAAM;IAC7B,IAAI,KAAK,MAAM,MAAM,MAAM,GAAG,KAAK,MAAM,GAAG;SACvC,IAAI,KAAK,KAAK,MAAM,IAAI,CAAC;EAChC;EACA,OAAO,MAAM,IAAI,CAAC;AACpB,EAAC;AAED,IAAI,MAAM,CAAC,MAAc,MAAc,MACrC;OAAI,KAAK,QAAQ,CAAC,IAAI,OAAO,CAAC,CAAC,E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ACF,EAAC;AAED,8EAA8E,GAC9E,OAAO,IAAI,SAAS,OAClB,KACA,SACA,KAAmB,KAAK,GACA,CAAC,MAAM,YAAY,KAAK,GAAG,EAAE,IAAI,CAAC,SAAQ;AAEpE,+DAA+D;AAC/D,IAAI,OAAO,OAAO,GAAa;EAC7B,IAAI,OAAO,MAAM,EAAE,IAAI;EACvB,IAAI;EACJ,IAAI;IACF,IAAI,OAAO,KAAK,KAAK,CAAC;IACtB,UAAU,KAAK,OAAO,IAAI,KAAK,KAAK,EAAE,WAAW,KAAK,KAAK;EAC7D,EAAE,OAAM,CAAiB;EACzB,OAAO,IAAI,MACT,OAAO,WAAW,WAAW,UAAU,GAAG,MAAM,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;AAExE;AAEA;8CAC8C,GAC9C,OAAO,IAAI,QAAQ,CAAC,KAAa,KAAmB,KAAK,GAAY,CAAC;IACpE,OAAO,OAAU;MACf,IAAI,IAAI,MAAM,GAAG,GAAG,IAAI,OAAO,EAAE,QAAQ;MACzC,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,MAAM,KAAK,GAAG;MAC/B,OAAO,MAAM,EAAE,IAAI;IACrB;IACA,OAAO,OAAO;MACZ,IAAI,IAAI,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,EAAE;QAC/B,QAAQ;QACR,SAAS;UAAE,gBAAgB;QAAmB;QAC9C,MAAM,KAAK,SAAS,CAAC;UAAE,UAAU;QAAQ;MAC3C;MACA,IAAI,CAAC,EAAE,EAAE,EAAE,MAAM,MAAM,KAAK,GAAG;MAC/B,OAAO,MAAM,EAAE,IAAI;IACrB;EACF,CAAC,EAAC"}
|
package/remote.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A kit-built app's view, loaded from the app itself, and its store over
|
|
3
|
+
* HTTP: what any host that draws the view outside a browser needs of the app.
|
|
4
|
+
* The iOS shell and `yak open` in a terminal are two such hosts; each hands
|
|
5
|
+
* in its own `fetch` (the phone's signed-in cookie, the CLI's token) and the
|
|
6
|
+
* packages it carries.
|
|
7
|
+
*
|
|
8
|
+
* An app names its view in its front page,
|
|
9
|
+
* `<link rel="yaks-native" href="view.ts">`. `viewAt` fetches that module and
|
|
10
|
+
* what it imports by relative path, compiles each from TypeScript, and runs
|
|
11
|
+
* it with the host's own copies of the app's `"platform"` packages standing
|
|
12
|
+
* in for them, as the platform's compile supplies them to its page; the
|
|
13
|
+
* host's copies are the ones its painter's DOM is rendered by. The module's
|
|
14
|
+
* default export is a component that takes the app's `store`.
|
|
15
|
+
*
|
|
16
|
+
* @module
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { transform } from 'sucrase'
|
|
20
|
+
/* @ts-types="npm:@types/react@19.3.0" */ import type { ComponentType } from 'npm:react@19.3.0'
|
|
21
|
+
|
|
22
|
+
/** The packages a host carries for a remote view, by the names it imports
|
|
23
|
+
* them by: `{ preact, 'preact/hooks': hooks, '@yaks/ui': ui }`. */
|
|
24
|
+
export type Carried = Record<string, unknown>
|
|
25
|
+
|
|
26
|
+
/** A view module's component: it draws the app over its store. */
|
|
27
|
+
export type NativeView = ComponentType<{ store: Store }>
|
|
28
|
+
|
|
29
|
+
/** An app's store, as a page's `./api/client.js` offers it. */
|
|
30
|
+
export type Store = {
|
|
31
|
+
query: <T = unknown>(filter: string) => Promise<T[]>
|
|
32
|
+
apply: (bundles: object[]) => Promise<unknown>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The app's view could not be loaded; its web page still opens. */
|
|
36
|
+
export class Unloadable extends Error {}
|
|
37
|
+
|
|
38
|
+
/** A path against the URL it is named from, as a browser resolves an href:
|
|
39
|
+
* absolute, from the origin, or beside the file.
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { assertEquals } from '@std/assert'
|
|
43
|
+
* let page = 'https://a.yaks.app/packing/'
|
|
44
|
+
* assertEquals(against(page, 'view.ts'), 'https://a.yaks.app/packing/view.ts')
|
|
45
|
+
* assertEquals(against(page + 'lib/a.ts', '../b.ts'), page + 'b.ts')
|
|
46
|
+
* assertEquals(against(page, '/x/'), 'https://a.yaks.app/x/')
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export let against = (from: string, path: string): string => {
|
|
50
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(path)) return path
|
|
51
|
+
let origin = from.match(/^[a-z][a-z0-9+.-]*:\/\/[^/]+/i)?.[0] ?? ''
|
|
52
|
+
if (path.startsWith('/')) return origin + path
|
|
53
|
+
let parts = from.split('/').slice(0, -1)
|
|
54
|
+
for (let p of path.split('/')) {
|
|
55
|
+
if (p == '..') parts.length > 3 && parts.pop()
|
|
56
|
+
else if (p != '.') parts.push(p)
|
|
57
|
+
}
|
|
58
|
+
return parts.join('/')
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let tag = (html: string, name: string, rel?: string) =>
|
|
62
|
+
[...html.matchAll(new RegExp(`<${name}\\b[^>]*>`, 'gi'))].map((m) => m[0])
|
|
63
|
+
.find((t) => !rel || new RegExp(`\\brel=["']?${rel}["'\\s>]`, 'i').test(t))
|
|
64
|
+
|
|
65
|
+
let href = (t: string | undefined) =>
|
|
66
|
+
t && /\bhref=["']?([^"'\s>]+)/i.exec(t)?.[1]
|
|
67
|
+
|
|
68
|
+
/** The URL of the view a page names, against its `<base>` when it has one,
|
|
69
|
+
* or null when it names none.
|
|
70
|
+
*
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { assertEquals } from '@std/assert'
|
|
73
|
+
* let html = '<base href="/packing/"><link rel="yaks-native" href="view.ts">'
|
|
74
|
+
* assertEquals(
|
|
75
|
+
* viewOf(html, 'https://a.yaks.app/packing'),
|
|
76
|
+
* 'https://a.yaks.app/packing/view.ts',
|
|
77
|
+
* )
|
|
78
|
+
* assertEquals(viewOf('<link rel="icon" href="i.png">', 'https://a/'), null)
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export let viewOf = (html: string, page: string): string | null => {
|
|
82
|
+
let view = href(tag(html, 'link', 'yaks-native'))
|
|
83
|
+
if (!view) return null
|
|
84
|
+
let base = href(tag(html, 'base'))
|
|
85
|
+
return against(base ? against(page, base) : page, view)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The URL of the view an app's page names, or null when it names none. */
|
|
89
|
+
export let nativeOf = async (
|
|
90
|
+
page: string,
|
|
91
|
+
go: typeof fetch = fetch,
|
|
92
|
+
): Promise<string | null> => {
|
|
93
|
+
let r = await go(page)
|
|
94
|
+
if (!r.ok) {
|
|
95
|
+
await r.body?.cancel()
|
|
96
|
+
return null
|
|
97
|
+
}
|
|
98
|
+
return viewOf(await r.text(), r.url || page)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type Module = { exports: Record<string, unknown> }
|
|
102
|
+
|
|
103
|
+
type Source = { code: string; names: string[] }
|
|
104
|
+
|
|
105
|
+
let gather = async (
|
|
106
|
+
url: string,
|
|
107
|
+
go: typeof fetch,
|
|
108
|
+
sources: Map<string, Source | undefined>,
|
|
109
|
+
): Promise<void> => {
|
|
110
|
+
if (sources.has(url)) return
|
|
111
|
+
sources.set(url, undefined)
|
|
112
|
+
let r = await go(url)
|
|
113
|
+
if (!r.ok) {
|
|
114
|
+
await r.body?.cancel()
|
|
115
|
+
throw new Unloadable(`${url} answered ${r.status}`)
|
|
116
|
+
}
|
|
117
|
+
let { code } = transform(await r.text(), {
|
|
118
|
+
transforms: ['typescript', 'imports'],
|
|
119
|
+
filePath: url,
|
|
120
|
+
})
|
|
121
|
+
let names = [...code.matchAll(/require\(['"]([^'"]+)['"]\)/g)].map((m) =>
|
|
122
|
+
m[1]
|
|
123
|
+
)
|
|
124
|
+
sources.set(url, { code, names })
|
|
125
|
+
for (let name of names) {
|
|
126
|
+
if (name.startsWith('.')) await gather(against(url, name), go, sources)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let execute = (
|
|
131
|
+
url: string,
|
|
132
|
+
carried: Carried,
|
|
133
|
+
sources: Map<string, Source | undefined>,
|
|
134
|
+
modules: Map<string, Module>,
|
|
135
|
+
): Module => {
|
|
136
|
+
let had = modules.get(url)
|
|
137
|
+
if (had) return had
|
|
138
|
+
let module: Module = { exports: {} }
|
|
139
|
+
modules.set(url, module)
|
|
140
|
+
let need = (name: string) =>
|
|
141
|
+
name.startsWith('.')
|
|
142
|
+
? execute(against(url, name), carried, sources, modules).exports
|
|
143
|
+
: carried[name]
|
|
144
|
+
new Function('require', 'module', 'exports', sources.get(url)!.code)(
|
|
145
|
+
need,
|
|
146
|
+
module,
|
|
147
|
+
module.exports,
|
|
148
|
+
)
|
|
149
|
+
return module
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Fetch a view's module graph so its host can select the renderer before it runs. */
|
|
153
|
+
export let prepareView = async (
|
|
154
|
+
url: string,
|
|
155
|
+
go: typeof fetch = fetch,
|
|
156
|
+
): Promise<{ imports: string[]; open: (carried: Carried) => NativeView }> => {
|
|
157
|
+
let sources = new Map<string, Source | undefined>()
|
|
158
|
+
await gather(url, go, sources)
|
|
159
|
+
let names = [
|
|
160
|
+
...new Set(
|
|
161
|
+
[...sources.values()].flatMap((s) =>
|
|
162
|
+
s!.names.filter((name) => !name.startsWith('.'))
|
|
163
|
+
),
|
|
164
|
+
),
|
|
165
|
+
]
|
|
166
|
+
return {
|
|
167
|
+
imports: names,
|
|
168
|
+
open(carried: Carried): NativeView {
|
|
169
|
+
for (let name of names) {
|
|
170
|
+
if (!(name in carried)) {
|
|
171
|
+
throw new Unloadable(
|
|
172
|
+
`it imports ${name}, which this host does not carry`,
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
let view = execute(url, carried, sources, new Map()).exports.default
|
|
177
|
+
if (typeof view != 'function') {
|
|
178
|
+
throw new Unloadable(`${url} exports no default view`)
|
|
179
|
+
}
|
|
180
|
+
return view as NativeView
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Load a view module and what it imports, its platform packages the host's. */
|
|
186
|
+
export let viewAt = async (
|
|
187
|
+
url: string,
|
|
188
|
+
carried: Carried,
|
|
189
|
+
go: typeof fetch = fetch,
|
|
190
|
+
): Promise<NativeView> => (await prepareView(url, go)).open(carried)
|
|
191
|
+
|
|
192
|
+
// What a refusal says: the store's own words, else its status.
|
|
193
|
+
let said = async (r: Response, doing: string) => {
|
|
194
|
+
let body = await r.text()
|
|
195
|
+
let message: string | undefined
|
|
196
|
+
try {
|
|
197
|
+
let json = JSON.parse(body)
|
|
198
|
+
message = json.message ?? json.error?.message ?? json.error
|
|
199
|
+
} catch { /* not JSON */ }
|
|
200
|
+
return new Error(
|
|
201
|
+
typeof message == 'string' ? message : `${doing} (HTTP ${r.status})`,
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** An app's store over HTTP, at its `api` (`https://a.yaks.app/packing/api`),
|
|
206
|
+
* as its page's `./api/client.js` offers it. */
|
|
207
|
+
export let store = (api: string, go: typeof fetch = fetch): Store => ({
|
|
208
|
+
query: async <T>(filter: string) => {
|
|
209
|
+
let r = await go(`${api}/query?${filter}`)
|
|
210
|
+
if (!r.ok) throw await said(r, 'Could not read this app')
|
|
211
|
+
return await r.json() as T[]
|
|
212
|
+
},
|
|
213
|
+
apply: async (bundles) => {
|
|
214
|
+
let r = await go(`${api}/apply`, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers: { 'content-type': 'application/json' },
|
|
217
|
+
body: JSON.stringify({ entities: bundles }),
|
|
218
|
+
})
|
|
219
|
+
if (!r.ok) throw await said(r, 'Could not save in this app')
|
|
220
|
+
return await r.json()
|
|
221
|
+
},
|
|
222
|
+
})
|