@civet/common 1.0.0
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 +21 -0
- package/README.md +4 -0
- package/dist/FetchProvider.d.ts +23 -0
- package/dist/SSEReceiver.d.ts +16 -0
- package/dist/main.d.ts +4 -0
- package/dist/main.js +82 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aaron Burmeister
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DataProvider, Meta, AbortSignalProxy } from '@civet/core';
|
|
2
|
+
export type FetchProviderOptions<Item = unknown, MetaType extends Meta = Meta, ResponseType extends Item | Item[] | void = Item | Item[] | void> = {
|
|
3
|
+
baseURL?: string;
|
|
4
|
+
modifyRequest?: (url: URL, request: RequestInit, meta: MetaType) => Promise<void> | void;
|
|
5
|
+
getResponse?: (url: URL, request: RequestInit, response: Response, meta: MetaType) => Promise<ResponseType> | ResponseType;
|
|
6
|
+
handleError?: (url: URL, request: RequestInit, response: Response, meta: MetaType) => Promise<ResponseType> | ResponseType;
|
|
7
|
+
};
|
|
8
|
+
export type FetchOptions<Item = unknown, MetaType extends Meta = Meta, ResponseType extends Item | Item[] = Item | Item[]> = {
|
|
9
|
+
json?: boolean;
|
|
10
|
+
noJson?: boolean;
|
|
11
|
+
noText?: boolean;
|
|
12
|
+
getResponse?: (url: URL, request: RequestInit, response: Response, meta: MetaType) => Promise<ResponseType> | ResponseType;
|
|
13
|
+
handleError?: (url: URL, request: RequestInit, response: Response, meta: MetaType) => Promise<ResponseType> | ResponseType;
|
|
14
|
+
};
|
|
15
|
+
export default class FetchProvider<Item = unknown, ResponseType extends Item | Item[] = Item | Item[], Query extends RequestInit | undefined = RequestInit | undefined, MetaType extends Meta = Meta, Options extends FetchOptions<Item, MetaType, ResponseType> = FetchOptions<Item, MetaType, ResponseType>> extends DataProvider<Item, Query, Options, MetaType, ResponseType> {
|
|
16
|
+
private options;
|
|
17
|
+
constructor(options?: FetchProviderOptions<Item, MetaType, ResponseType>);
|
|
18
|
+
handleGet(resource: string, query: Query & {
|
|
19
|
+
method?: RequestInit['method'];
|
|
20
|
+
}, options: Options | undefined, meta: MetaType, abortSignal: AbortSignalProxy): Promise<ResponseType>;
|
|
21
|
+
request<ResponseTypeI extends ResponseType = ResponseType, QueryI extends Query = Query, OptionsI extends Options = Options, MetaTypeI extends MetaType = MetaType>(resource: string, query: QueryI, options?: OptionsI | undefined, meta?: MetaTypeI, abortSignal?: AbortSignalProxy): Promise<ResponseTypeI>;
|
|
22
|
+
}
|
|
23
|
+
export type FetchProviderType = InstanceType<typeof FetchProvider>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { GenericDataProvider, ResourceContextValue } from '@civet/core';
|
|
2
|
+
import { EventReceiver } from '@civet/events';
|
|
3
|
+
export type SSEReceiverOptions<Resource extends ResourceContextValue<GenericDataProvider>, EventType = MessageEvent> = {
|
|
4
|
+
getEvents?: (resource: Resource, type: string, event: MessageEvent) => Promise<EventType[]> | EventType[];
|
|
5
|
+
};
|
|
6
|
+
export type SSEOptions<Resource extends ResourceContextValue<GenericDataProvider>, EventType = MessageEvent> = {
|
|
7
|
+
events?: string[];
|
|
8
|
+
getEvents?: (resource: Resource, type: string, event: MessageEvent) => Promise<EventType[]> | EventType[];
|
|
9
|
+
};
|
|
10
|
+
export default class SSEReceiver<Resource extends ResourceContextValue<GenericDataProvider>, EventType = MessageEvent, Options extends SSEOptions<Resource, EventType> = SSEOptions<Resource, EventType>> extends EventReceiver<Resource, EventType, Options> {
|
|
11
|
+
readonly eventSource: EventSource;
|
|
12
|
+
private options;
|
|
13
|
+
constructor(eventSource: EventSource, options?: SSEReceiverOptions<Resource, EventType>);
|
|
14
|
+
handleSubscribe(resource: Resource, options: Options | undefined, handler: (events: EventType[]) => void): () => void;
|
|
15
|
+
}
|
|
16
|
+
export type SSEReceiverType = InstanceType<typeof SSEReceiver>;
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { default as FetchProvider } from './FetchProvider';
|
|
2
|
+
export type { FetchOptions, FetchProviderOptions, FetchProviderType, } from './FetchProvider';
|
|
3
|
+
export { default as SSEReceiver } from './SSEReceiver';
|
|
4
|
+
export type { SSEOptions, SSEReceiverOptions, SSEReceiverType, } from './SSEReceiver';
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { DataProvider as p, Meta as l } from "@civet/core";
|
|
2
|
+
import { EventReceiver as d } from "@civet/events";
|
|
3
|
+
class v extends p {
|
|
4
|
+
options;
|
|
5
|
+
constructor(n = {}) {
|
|
6
|
+
super(), this.options = n;
|
|
7
|
+
}
|
|
8
|
+
handleGet(n, r, t, e, c) {
|
|
9
|
+
return this.request(n, r, t, e, c);
|
|
10
|
+
}
|
|
11
|
+
async request(n, r, t, e, c) {
|
|
12
|
+
e = e instanceof l ? e : new l(e);
|
|
13
|
+
const i = new AbortController();
|
|
14
|
+
c?.listen(i.abort.bind(i));
|
|
15
|
+
const s = new URL(n, this.options.baseURL), u = new Headers(r?.headers), a = { ...r, headers: u };
|
|
16
|
+
await this.options.modifyRequest?.(s, a, e);
|
|
17
|
+
const o = await fetch(s.toString(), {
|
|
18
|
+
...a,
|
|
19
|
+
signal: AbortSignal.any(
|
|
20
|
+
[a.signal, i.signal].filter((g) => g != null)
|
|
21
|
+
)
|
|
22
|
+
});
|
|
23
|
+
if (!o.ok) {
|
|
24
|
+
if (t?.handleError)
|
|
25
|
+
return t.handleError(
|
|
26
|
+
s,
|
|
27
|
+
a,
|
|
28
|
+
o,
|
|
29
|
+
e
|
|
30
|
+
);
|
|
31
|
+
if (this.options.handleError)
|
|
32
|
+
return this.options.handleError(
|
|
33
|
+
s,
|
|
34
|
+
a,
|
|
35
|
+
o,
|
|
36
|
+
e
|
|
37
|
+
);
|
|
38
|
+
throw new Error(o.statusText);
|
|
39
|
+
}
|
|
40
|
+
if (t?.getResponse)
|
|
41
|
+
return t.getResponse(
|
|
42
|
+
s,
|
|
43
|
+
a,
|
|
44
|
+
o,
|
|
45
|
+
e
|
|
46
|
+
);
|
|
47
|
+
if (this.options.getResponse)
|
|
48
|
+
return this.options.getResponse(
|
|
49
|
+
s,
|
|
50
|
+
a,
|
|
51
|
+
o,
|
|
52
|
+
e
|
|
53
|
+
);
|
|
54
|
+
if (t?.json || !t?.noJson && /^application\/[^+]*[+]?(json);?.*$/.test(
|
|
55
|
+
o.headers.get("Content-Type") ?? ""
|
|
56
|
+
))
|
|
57
|
+
return o.json();
|
|
58
|
+
if (!t?.noText) return o.text();
|
|
59
|
+
throw new Error("unprocessable response");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class w extends d {
|
|
63
|
+
eventSource;
|
|
64
|
+
options;
|
|
65
|
+
constructor(n, r = {}) {
|
|
66
|
+
super(), this.eventSource = n, this.options = r;
|
|
67
|
+
}
|
|
68
|
+
handleSubscribe(n, r, t) {
|
|
69
|
+
const e = new AbortController();
|
|
70
|
+
return (r?.events?.length ? r.events : ["message"]).forEach((i) => {
|
|
71
|
+
this.eventSource.addEventListener(
|
|
72
|
+
i,
|
|
73
|
+
async (s) => r?.getEvents ? t(await r.getEvents(n, i, s)) : this.options.getEvents ? t(await this.options.getEvents(n, i, s)) : t([s]),
|
|
74
|
+
{ signal: e.signal }
|
|
75
|
+
);
|
|
76
|
+
}), e.abort.bind(e);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
v as FetchProvider,
|
|
81
|
+
w as SSEReceiver
|
|
82
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@civet/common",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Civet",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Aaron Burmeister"
|
|
7
|
+
},
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/civet-org/common.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/civet-org/common/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://civet.js.org/",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"civet",
|
|
20
|
+
"react",
|
|
21
|
+
"data",
|
|
22
|
+
"rest"
|
|
23
|
+
],
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dist/main.js",
|
|
28
|
+
"types": "./dist/main.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/main.d.ts",
|
|
33
|
+
"default": "./dist/main.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"dev": "vite",
|
|
39
|
+
"build": "tsc -b ./tsconfig-build.json && vite build",
|
|
40
|
+
"lint": "eslint . --max-warnings 0",
|
|
41
|
+
"lint:fix": "npm run lint -- --fix",
|
|
42
|
+
"prettify": "prettier . --write",
|
|
43
|
+
"preview": "vite preview",
|
|
44
|
+
"postversion": "git commit -am$npm_package_version",
|
|
45
|
+
"prepublishOnly": "npm run build"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@civet/core": ">=6.0",
|
|
49
|
+
"@civet/events": ">=3.0",
|
|
50
|
+
"react": ">=18.0",
|
|
51
|
+
"react-dom": ">=18.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@civet/core": "^6.0.2",
|
|
55
|
+
"@civet/events": "^3.0.1",
|
|
56
|
+
"@eslint/js": "^9.35.0",
|
|
57
|
+
"@types/react": "^19.1.12",
|
|
58
|
+
"@types/react-dom": "^19.1.9",
|
|
59
|
+
"@vitejs/plugin-react": "^5.0.2",
|
|
60
|
+
"eslint": "^9.35.0",
|
|
61
|
+
"eslint-config-prettier": "^10.1.8",
|
|
62
|
+
"eslint-import-resolver-alias": "^1.1.2",
|
|
63
|
+
"eslint-import-resolver-typescript": "^4.4.4",
|
|
64
|
+
"eslint-plugin-import": "^2.32.0",
|
|
65
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
66
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
67
|
+
"eslint-plugin-react-refresh": "^0.5.0",
|
|
68
|
+
"eslint-plugin-unused-imports": "^4.2.0",
|
|
69
|
+
"globals": "^17.3.0",
|
|
70
|
+
"prettier": "^3.6.2",
|
|
71
|
+
"react": "^19.1.1",
|
|
72
|
+
"react-dom": "^19.1.1",
|
|
73
|
+
"typescript": "^5.9.2",
|
|
74
|
+
"typescript-eslint": "^8.43.0",
|
|
75
|
+
"vite": "^7.1.5",
|
|
76
|
+
"vite-plugin-dts": "^4.5.4",
|
|
77
|
+
"vite-tsconfig-paths": "^6.1.1"
|
|
78
|
+
}
|
|
79
|
+
}
|