@fabriktor/client 0.0.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.
- package/LICENSE +13 -0
- package/README.md +29 -0
- package/dist/src/core/err.d.ts +13 -0
- package/dist/src/core/err.js +20 -0
- package/dist/src/core/http.d.ts +31 -0
- package/dist/src/core/http.js +65 -0
- package/dist/src/core/path.d.ts +7 -0
- package/dist/src/core/path.js +15 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +4 -0
- package/dist/src/users/users.d.ts +25 -0
- package/dist/src/users/users.js +43 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2025 Fabriktor, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Client
|
|
2
|
+
|
|
3
|
+
![Fabriktor Logo][fabriktor-logo]
|
|
4
|
+
|
|
5
|
+
[![GitLab stars][stars-badge]][starrers-link]
|
|
6
|
+
|
|
7
|
+
[![License][license-badge]][license-link]
|
|
8
|
+
[![Pipeline Badge][pipeline-badge]][pipelines-link]
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Overview
|
|
13
|
+
|
|
14
|
+
This package is designed exclusively for Fabriktor's frontend operational needs and is
|
|
15
|
+
not intended for public usage.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## License
|
|
20
|
+
|
|
21
|
+
This project is licensed under the [Apache License 2.0][license-link].
|
|
22
|
+
|
|
23
|
+
[fabriktor-logo]: https://backend.fabriktor.com/filehub/img/gitlab/truck_yellow_filling.gif
|
|
24
|
+
[stars-badge]: https://badgen.net/gitlab/stars/fabriktor/client?icon=gitlab&color=orange
|
|
25
|
+
[license-badge]: https://badgen.net/badge/license/Apache-2.0/orange
|
|
26
|
+
[pipeline-badge]: https://gitlab.com/fabriktor/client/badges/main/pipeline.svg
|
|
27
|
+
[starrers-link]: https://gitlab.com/fabriktor/client/-/starrers
|
|
28
|
+
[license-link]: https://gitlab.com/fabriktor/client/-/raw/main/LICENSE
|
|
29
|
+
[pipelines-link]: https://gitlab.com/fabriktor/client/-/pipelines
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { HTTPErr, WrenchErr } from "@fabriktor/schema";
|
|
2
|
+
export type ClientErrOptions = {
|
|
3
|
+
code: number;
|
|
4
|
+
msg: string;
|
|
5
|
+
app_err?: WrenchErr;
|
|
6
|
+
};
|
|
7
|
+
export declare class ClientErr extends Error {
|
|
8
|
+
code: number;
|
|
9
|
+
app_err?: WrenchErr;
|
|
10
|
+
constructor(opts: ClientErrOptions);
|
|
11
|
+
}
|
|
12
|
+
export declare function newClientErr(opts: ClientErrOptions): ClientErr;
|
|
13
|
+
export declare function newClientErrFromHTTP(err: HTTPErr): ClientErr;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class ClientErr extends Error {
|
|
2
|
+
code;
|
|
3
|
+
app_err;
|
|
4
|
+
constructor(opts) {
|
|
5
|
+
super(opts.msg);
|
|
6
|
+
this.name = new.target.name;
|
|
7
|
+
this.code = opts.code;
|
|
8
|
+
this.app_err = opts.app_err;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function newClientErr(opts) {
|
|
12
|
+
return new ClientErr(opts);
|
|
13
|
+
}
|
|
14
|
+
export function newClientErrFromHTTP(err) {
|
|
15
|
+
return newClientErr({
|
|
16
|
+
code: err.code,
|
|
17
|
+
msg: err.msg,
|
|
18
|
+
app_err: err.app_err,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { HTTPMethodDelete, HTTPMethodGet, HTTPMethodPatch, HTTPMethodPost } from "@fabriktor/schema";
|
|
2
|
+
import type { OperationResult } from "@fabriktor/schema";
|
|
3
|
+
export type Method = typeof HTTPMethodGet | typeof HTTPMethodPost | typeof HTTPMethodPatch | typeof HTTPMethodDelete;
|
|
4
|
+
export type Op<T> = Omit<OperationResult, "value"> & {
|
|
5
|
+
value?: T;
|
|
6
|
+
};
|
|
7
|
+
export type HTTPOptions<T> = {
|
|
8
|
+
base_url: string;
|
|
9
|
+
path: string;
|
|
10
|
+
method: Method;
|
|
11
|
+
token?: string;
|
|
12
|
+
body?: T;
|
|
13
|
+
};
|
|
14
|
+
export interface Fetcher {
|
|
15
|
+
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
16
|
+
}
|
|
17
|
+
export interface HTTPDoer {
|
|
18
|
+
do<T, R>(opts: HTTPOptions<T>): Promise<R>;
|
|
19
|
+
}
|
|
20
|
+
export type HTTPClientOptions = {
|
|
21
|
+
fetcher: Fetcher;
|
|
22
|
+
};
|
|
23
|
+
export declare class WebFetcher implements Fetcher {
|
|
24
|
+
fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
25
|
+
}
|
|
26
|
+
export declare class HTTPClient implements HTTPDoer {
|
|
27
|
+
private fetcher;
|
|
28
|
+
constructor(opts: HTTPClientOptions);
|
|
29
|
+
do<T, R>(opts: HTTPOptions<T>): Promise<R>;
|
|
30
|
+
}
|
|
31
|
+
export declare function newHTTPClient(opts?: Partial<HTTPClientOptions>): HTTPClient;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { MimeJSON, } from "@fabriktor/schema";
|
|
2
|
+
import { newClientErrFromHTTP } from "./err";
|
|
3
|
+
const headerAuthorization = "authorization";
|
|
4
|
+
const headerContentType = "content-type";
|
|
5
|
+
const prefixBearer = "Bearer";
|
|
6
|
+
export class WebFetcher {
|
|
7
|
+
async fetch(input, init) {
|
|
8
|
+
return await fetch(input, init);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class HTTPClient {
|
|
12
|
+
fetcher;
|
|
13
|
+
constructor(opts) {
|
|
14
|
+
this.fetcher = opts.fetcher;
|
|
15
|
+
}
|
|
16
|
+
async do(opts) {
|
|
17
|
+
const res = await this.fetcher.fetch(url(opts.base_url, opts.path), {
|
|
18
|
+
method: opts.method,
|
|
19
|
+
headers: headers(opts),
|
|
20
|
+
body: body(opts.body),
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
return await fail(res);
|
|
24
|
+
}
|
|
25
|
+
if (res.body === null) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return (await res.json());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function newHTTPClient(opts) {
|
|
32
|
+
return new HTTPClient({
|
|
33
|
+
fetcher: opts?.fetcher ?? new WebFetcher(),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function url(base, path) {
|
|
37
|
+
return `${base.replace(/\/+$/g, "")}/${path.replace(/^\/+/g, "")}`;
|
|
38
|
+
}
|
|
39
|
+
function headers(opts) {
|
|
40
|
+
const h = new Headers();
|
|
41
|
+
if (opts.body !== undefined) {
|
|
42
|
+
h.set(headerContentType, MimeJSON);
|
|
43
|
+
}
|
|
44
|
+
if (opts.token !== undefined) {
|
|
45
|
+
h.set(headerAuthorization, `${prefixBearer} ${opts.token}`);
|
|
46
|
+
}
|
|
47
|
+
return h;
|
|
48
|
+
}
|
|
49
|
+
function body(v) {
|
|
50
|
+
return v === undefined ? undefined : JSON.stringify(v);
|
|
51
|
+
}
|
|
52
|
+
async function fail(res) {
|
|
53
|
+
const txt = await res.text();
|
|
54
|
+
let out;
|
|
55
|
+
try {
|
|
56
|
+
out = JSON.parse(txt);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
out = {
|
|
60
|
+
code: res.status,
|
|
61
|
+
msg: txt || res.statusText || res.status.toString(),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
throw newClientErrFromHTTP(out);
|
|
65
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PathActions } from "@fabriktor/schema";
|
|
2
|
+
const pathSep = "/";
|
|
3
|
+
function clean(v) {
|
|
4
|
+
return v.replace(/^\/+|\/+$/g, "");
|
|
5
|
+
}
|
|
6
|
+
export function buildPath(opts) {
|
|
7
|
+
const out = [opts.svc, opts.col].map(clean);
|
|
8
|
+
if (opts.id !== undefined) {
|
|
9
|
+
out.push(encodeURIComponent(opts.id));
|
|
10
|
+
}
|
|
11
|
+
if (opts.action !== undefined) {
|
|
12
|
+
out.push(PathActions, clean(opts.action));
|
|
13
|
+
}
|
|
14
|
+
return `${pathSep}${out.join(pathSep)}`;
|
|
15
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { InUser, OperationResult, UpsertResult } from "@fabriktor/schema";
|
|
2
|
+
import type { HTTPDoer, Op } from "../core/http";
|
|
3
|
+
export type AuthOptions = {
|
|
4
|
+
base_url: string;
|
|
5
|
+
token: string;
|
|
6
|
+
};
|
|
7
|
+
export type BootstrapOptions = AuthOptions & {
|
|
8
|
+
in: InUser;
|
|
9
|
+
};
|
|
10
|
+
export interface UsersOperator {
|
|
11
|
+
bootstrap(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
|
|
12
|
+
sync(opts: AuthOptions): Promise<OperationResult>;
|
|
13
|
+
sendEmailVerification(opts: AuthOptions): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export type UsersClientOptions = {
|
|
16
|
+
http: HTTPDoer;
|
|
17
|
+
};
|
|
18
|
+
export declare class UsersClient implements UsersOperator {
|
|
19
|
+
private http;
|
|
20
|
+
constructor(opts: UsersClientOptions);
|
|
21
|
+
bootstrap(opts: BootstrapOptions): Promise<Op<UpsertResult>>;
|
|
22
|
+
sync(opts: AuthOptions): Promise<OperationResult>;
|
|
23
|
+
sendEmailVerification(opts: AuthOptions): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export declare function newUsersClient(opts: UsersClientOptions): UsersClient;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ColUsersUsers, HTTPMethodPost, PathUsersBootstrap, PathUsersSendEmailVerification, PathUsersSync, SvcUsers, } from "@fabriktor/schema";
|
|
2
|
+
import { buildPath } from "../core/path";
|
|
3
|
+
export class UsersClient {
|
|
4
|
+
http;
|
|
5
|
+
constructor(opts) {
|
|
6
|
+
this.http = opts.http;
|
|
7
|
+
}
|
|
8
|
+
async bootstrap(opts) {
|
|
9
|
+
return await this.http.do({
|
|
10
|
+
base_url: opts.base_url,
|
|
11
|
+
path: userPath(PathUsersBootstrap),
|
|
12
|
+
method: HTTPMethodPost,
|
|
13
|
+
token: opts.token,
|
|
14
|
+
body: opts.in,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async sync(opts) {
|
|
18
|
+
return await this.http.do({
|
|
19
|
+
base_url: opts.base_url,
|
|
20
|
+
path: userPath(PathUsersSync),
|
|
21
|
+
method: HTTPMethodPost,
|
|
22
|
+
token: opts.token,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async sendEmailVerification(opts) {
|
|
26
|
+
return await this.http.do({
|
|
27
|
+
base_url: opts.base_url,
|
|
28
|
+
path: userPath(PathUsersSendEmailVerification),
|
|
29
|
+
method: HTTPMethodPost,
|
|
30
|
+
token: opts.token,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export function newUsersClient(opts) {
|
|
35
|
+
return new UsersClient(opts);
|
|
36
|
+
}
|
|
37
|
+
function userPath(action) {
|
|
38
|
+
return buildPath({
|
|
39
|
+
svc: SvcUsers,
|
|
40
|
+
col: ColUsersUsers,
|
|
41
|
+
action,
|
|
42
|
+
});
|
|
43
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fabriktor/client",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"types": "./dist/src/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"typescript": "^6.0.3"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@fabriktor/schema": "^0.0.10"
|
|
21
|
+
}
|
|
22
|
+
}
|