@opra/client 0.22.0 → 0.23.1
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/browser.js +120 -94
- package/cjs/client.js +24 -14
- package/cjs/collection-node.js +56 -42
- package/cjs/constants.js +3 -2
- package/cjs/http-request-observable.js +8 -9
- package/cjs/http-request.js +8 -10
- package/cjs/singleton-node.js +27 -22
- package/esm/client.js +25 -15
- package/esm/collection-node.js +57 -43
- package/esm/constants.js +2 -1
- package/esm/http-request-observable.js +6 -7
- package/esm/http-request.js +8 -10
- package/esm/singleton-node.js +28 -23
- package/package.json +3 -3
- package/types/client.d.ts +4 -4
- package/types/collection-node.d.ts +2 -2
- package/types/constants.d.ts +2 -1
- package/types/http-request-observable.d.ts +3 -8
- package/types/http-request.d.ts +7 -8
- package/types/singleton-node.d.ts +2 -2
- package/types/types.d.ts +7 -4
package/esm/constants.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const JSON_CONTENT_TYPE_PATTERN = /^application\/([\w-]+\+)?\bjson\b/i;
|
|
2
2
|
export const TEXT_CONTENT_TYPE_PATTERN = /^text\/.*$/i;
|
|
3
3
|
export const FORMDATA_CONTENT_TYPE_PATTERN = /^multipart\/\bform-data\b/i;
|
|
4
|
-
export const
|
|
4
|
+
export const kRequest = Symbol.for('kRequest');
|
|
5
|
+
export const kContext = Symbol.for('kContext');
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { lastValueFrom, Observable } from 'rxjs';
|
|
2
2
|
import { uid } from '@opra/common';
|
|
3
|
-
import {
|
|
3
|
+
import { kContext, kRequest } from './constants.js';
|
|
4
4
|
import { HttpRequest } from './http-request.js';
|
|
5
5
|
import { HttpObserveType } from './types.js';
|
|
6
|
-
const kRequest = Symbol('kRequest');
|
|
7
6
|
export class HttpRequestObservable extends Observable {
|
|
8
7
|
constructor(context, options) {
|
|
9
8
|
super((subscriber) => {
|
|
10
|
-
context.send(options?.observe || HttpObserveType.Body, this[kRequest])
|
|
9
|
+
context.send(options?.observe || HttpObserveType.Body, this[kRequest])
|
|
10
|
+
.subscribe((subscriber));
|
|
11
11
|
});
|
|
12
|
-
this[
|
|
12
|
+
this[kContext] = context;
|
|
13
13
|
this[kRequest] = new HttpRequest(options?.http);
|
|
14
14
|
this.contentId = uid(6);
|
|
15
15
|
}
|
|
@@ -26,12 +26,11 @@ export class HttpRequestObservable extends Observable {
|
|
|
26
26
|
return this;
|
|
27
27
|
}
|
|
28
28
|
async fetch(observe) {
|
|
29
|
-
return lastValueFrom(this[
|
|
29
|
+
return lastValueFrom(this[kContext]
|
|
30
|
+
.send(observe || HttpObserveType.Body, this[kRequest]));
|
|
30
31
|
}
|
|
31
32
|
with(cb) {
|
|
32
33
|
cb(this);
|
|
33
34
|
return this;
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
HttpRequestObservable.kContext = kHttpClientContext;
|
|
37
|
-
HttpRequestObservable.kRequest = kRequest;
|
package/esm/http-request.js
CHANGED
|
@@ -18,24 +18,22 @@ export class HttpRequest {
|
|
|
18
18
|
this.referrerPolicy = init?.referrerPolicy || '';
|
|
19
19
|
this.signal = init?.signal || new AbortController().signal;
|
|
20
20
|
this.body = init?.body;
|
|
21
|
-
this.
|
|
22
|
-
if (init?.params)
|
|
23
|
-
|
|
21
|
+
this.parsedUrl = new OpraURL(init?.url);
|
|
22
|
+
if (init?.params) {
|
|
23
|
+
const params = new URLSearchParams(init.params);
|
|
24
|
+
params.forEach((v, k) => this.params.set(k, v));
|
|
25
|
+
}
|
|
24
26
|
}
|
|
25
27
|
/** Returns the URL of request as a string. */
|
|
26
28
|
get url() {
|
|
27
|
-
return this.
|
|
29
|
+
return this.parsedUrl;
|
|
28
30
|
}
|
|
29
31
|
set url(value) {
|
|
30
|
-
this.
|
|
32
|
+
this.parsedUrl = value;
|
|
31
33
|
}
|
|
32
34
|
/** Returns the searchParams of the URL as OpraURLSearchParams */
|
|
33
35
|
get params() {
|
|
34
|
-
return this.
|
|
35
|
-
}
|
|
36
|
-
/** Returns the path part of URL as OpraURLPath */
|
|
37
|
-
get path() {
|
|
38
|
-
return this.urlInstance.path;
|
|
36
|
+
return this.parsedUrl.searchParams;
|
|
39
37
|
}
|
|
40
38
|
clone(...update) {
|
|
41
39
|
const out = new HttpRequest();
|
package/esm/singleton-node.js
CHANGED
|
@@ -1,59 +1,64 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { toArrayDef } from 'putil-varhelpers';
|
|
2
|
+
import { kContext, kRequest } from './constants.js';
|
|
2
3
|
import { HttpRequestObservable } from './http-request-observable.js';
|
|
3
4
|
export class HttpSingletonNode {
|
|
4
5
|
constructor(context) {
|
|
5
|
-
this[
|
|
6
|
+
this[kContext] = context;
|
|
6
7
|
}
|
|
7
8
|
create(data, options) {
|
|
8
|
-
const context = this[
|
|
9
|
+
const context = this[kContext];
|
|
10
|
+
context.operation = 'create';
|
|
9
11
|
const requestHost = new HttpRequestObservable(context, options);
|
|
10
|
-
const request = requestHost[
|
|
12
|
+
const request = requestHost[kRequest];
|
|
11
13
|
request.method = 'POST';
|
|
12
|
-
request.url
|
|
14
|
+
request.url.join(context.resourceName);
|
|
13
15
|
request.body = data;
|
|
14
16
|
if (options?.include)
|
|
15
|
-
request.params.set('$include', options.include);
|
|
17
|
+
request.params.set('$include', toArrayDef(options.include, []).join(','));
|
|
16
18
|
if (options?.pick)
|
|
17
|
-
request.params.set('$pick', options.pick);
|
|
19
|
+
request.params.set('$pick', toArrayDef(options.pick, []).join(','));
|
|
18
20
|
if (options?.omit)
|
|
19
|
-
request.params.set('$omit', options.omit);
|
|
21
|
+
request.params.set('$omit', toArrayDef(options.omit, []).join(','));
|
|
20
22
|
return requestHost;
|
|
21
23
|
}
|
|
22
24
|
delete(options) {
|
|
23
|
-
const context = this[
|
|
25
|
+
const context = this[kContext];
|
|
26
|
+
context.operation = 'delete';
|
|
24
27
|
const requestHost = new HttpRequestObservable(context, options);
|
|
25
|
-
const request = requestHost[
|
|
28
|
+
const request = requestHost[kRequest];
|
|
26
29
|
request.method = 'DELETE';
|
|
27
|
-
request.
|
|
30
|
+
request.url.join({ resource: context.resourceName });
|
|
28
31
|
return requestHost;
|
|
29
32
|
}
|
|
30
33
|
get(options) {
|
|
31
|
-
const context = this[
|
|
34
|
+
const context = this[kContext];
|
|
35
|
+
context.operation = 'get';
|
|
32
36
|
const requestHost = new HttpRequestObservable(context, options);
|
|
33
|
-
const request = requestHost[
|
|
37
|
+
const request = requestHost[kRequest];
|
|
34
38
|
request.method = 'GET';
|
|
35
|
-
request.
|
|
39
|
+
request.url.join({ resource: context.resourceName });
|
|
36
40
|
if (options?.include)
|
|
37
|
-
request.params.set('$include', options.include);
|
|
41
|
+
request.params.set('$include', toArrayDef(options.include, []).join(','));
|
|
38
42
|
if (options?.pick)
|
|
39
|
-
request.params.set('$pick', options.pick);
|
|
43
|
+
request.params.set('$pick', toArrayDef(options.pick, []).join(','));
|
|
40
44
|
if (options?.omit)
|
|
41
|
-
request.params.set('$omit', options.omit);
|
|
45
|
+
request.params.set('$omit', toArrayDef(options.omit, []).join(','));
|
|
42
46
|
return requestHost;
|
|
43
47
|
}
|
|
44
48
|
update(data, options) {
|
|
45
|
-
const context = this[
|
|
49
|
+
const context = this[kContext];
|
|
50
|
+
context.operation = 'update';
|
|
46
51
|
const requestHost = new HttpRequestObservable(context, options);
|
|
47
|
-
const request = requestHost[
|
|
52
|
+
const request = requestHost[kRequest];
|
|
48
53
|
request.method = 'PATCH';
|
|
49
|
-
request.
|
|
54
|
+
request.url.join({ resource: context.resourceName });
|
|
50
55
|
request.body = data;
|
|
51
56
|
if (options?.include)
|
|
52
|
-
request.params.set('$include', options.include);
|
|
57
|
+
request.params.set('$include', toArrayDef(options.include, []).join(','));
|
|
53
58
|
if (options?.pick)
|
|
54
|
-
request.params.set('$pick', options.pick);
|
|
59
|
+
request.params.set('$pick', toArrayDef(options.pick, []).join(','));
|
|
55
60
|
if (options?.omit)
|
|
56
|
-
request.params.set('$omit', options.omit);
|
|
61
|
+
request.params.set('$omit', toArrayDef(options.omit, []).join(','));
|
|
57
62
|
return requestHost;
|
|
58
63
|
}
|
|
59
64
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.1",
|
|
4
4
|
"description": "Opra Client package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@browsery/i18next": "^0.6.0",
|
|
47
47
|
"@browsery/stream": "^0.5.0",
|
|
48
48
|
"@browsery/util": "^0.4.0",
|
|
49
|
-
"@opra/common": "^0.
|
|
49
|
+
"@opra/common": "^0.23.1",
|
|
50
50
|
"accepts": "^1.3.8",
|
|
51
51
|
"buffer": "^6.0.3",
|
|
52
52
|
"cookie": "^0.5.0",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"process": "^0.11.10",
|
|
61
61
|
"putil-isplainobject": "^1.1.5",
|
|
62
62
|
"putil-merge": "^3.10.3",
|
|
63
|
-
"putil-promisify": "^1.10.
|
|
63
|
+
"putil-promisify": "^1.10.1",
|
|
64
64
|
"putil-varhelpers": "^1.6.5",
|
|
65
65
|
"reflect-metadata": "^0.1.13",
|
|
66
66
|
"rxjs": "^7.8.1",
|
package/types/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Observable, Subscriber } from 'rxjs';
|
|
2
2
|
import { StrictOmit, Type } from 'ts-gems';
|
|
3
|
-
import { ApiDocument,
|
|
3
|
+
import { ApiDocument, OpraSchema } from '@opra/common';
|
|
4
4
|
import { HttpCollectionNode } from './collection-node.js';
|
|
5
5
|
import { HttpRequest } from './http-request.js';
|
|
6
6
|
import { HttpResponse } from './http-response.js';
|
|
@@ -30,16 +30,16 @@ export declare class OpraHttpClient {
|
|
|
30
30
|
};
|
|
31
31
|
defaults: StrictOmit<HttpRequestDefaults, 'headers' | 'params'> & {
|
|
32
32
|
headers: Headers;
|
|
33
|
-
params:
|
|
33
|
+
params: URLSearchParams;
|
|
34
34
|
};
|
|
35
35
|
constructor(serviceUrl: string, options?: OpraHttpClientOptions);
|
|
36
36
|
get serviceUrl(): string;
|
|
37
37
|
getMetadata(): Promise<ApiDocument>;
|
|
38
38
|
collection<TType = any>(resourceName: string | Type<TType>): HttpCollectionNode<TType>;
|
|
39
39
|
singleton<TType = any>(resourceName: string | Type<TType>): HttpSingletonNode<TType>;
|
|
40
|
-
protected _sendRequest<TBody>(observe: HttpObserveType, request: HttpRequest, ctx?: HttpClientContext): Observable<HttpResponse<TBody> | TBody | HttpEvent>;
|
|
40
|
+
protected _sendRequest<TBody>(resourceKind: OpraSchema.Resource.Kind, operation: string, observe: HttpObserveType, request: HttpRequest, ctx?: HttpClientContext): Observable<HttpResponse<TBody> | TBody | HttpEvent>;
|
|
41
41
|
protected _fetch(url: string, init?: RequestInit): Promise<Response>;
|
|
42
42
|
protected _createResponse(init?: HttpResponse.Initiator): HttpResponse;
|
|
43
|
-
protected _handleResponse(observe: HttpObserveType, subscriber: Subscriber<any>, request: HttpRequest, fetchResponse: Response, ctx?: HttpClientContext): Promise<void>;
|
|
43
|
+
protected _handleResponse(resourceKind: OpraSchema.Resource.Kind, operation: string, observe: HttpObserveType, subscriber: Subscriber<any>, request: HttpRequest, fetchResponse: Response, ctx?: HttpClientContext): Promise<void>;
|
|
44
44
|
}
|
|
45
45
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StrictOmit } from 'ts-gems';
|
|
2
2
|
import type { PartialInput } from '@opra/common';
|
|
3
3
|
import { OpraFilter } from '@opra/common';
|
|
4
|
-
import {
|
|
4
|
+
import { kContext } from './constants.js';
|
|
5
5
|
import { HttpRequestObservable } from './http-request-observable.js';
|
|
6
6
|
import { HttpResponse } from './http-response.js';
|
|
7
7
|
import { HttpClientContext, HttpEvent, HttpObserveType } from './types.js';
|
|
@@ -48,7 +48,7 @@ export declare namespace HttpCollectionNode {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
export declare class HttpCollectionNode<TType, TResponseExt = {}> {
|
|
51
|
-
protected [
|
|
51
|
+
protected [kContext]: HttpClientContext;
|
|
52
52
|
constructor(context: HttpClientContext);
|
|
53
53
|
create(data: PartialInput<TType>, options?: StrictOmit<HttpCollectionNode.CreateOptions, 'observe'> & {
|
|
54
54
|
observe?: HttpObserveType.Body;
|
package/types/constants.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export declare const JSON_CONTENT_TYPE_PATTERN: RegExp;
|
|
2
2
|
export declare const TEXT_CONTENT_TYPE_PATTERN: RegExp;
|
|
3
3
|
export declare const FORMDATA_CONTENT_TYPE_PATTERN: RegExp;
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const kRequest: unique symbol;
|
|
5
|
+
export declare const kContext: unique symbol;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import {
|
|
3
|
-
import { kHttpClientContext } from './constants.js';
|
|
2
|
+
import { kContext, kRequest } from './constants.js';
|
|
4
3
|
import { HttpRequest } from './http-request.js';
|
|
5
4
|
import { HttpResponse } from './http-response.js';
|
|
6
5
|
import { HttpClientContext, HttpObserveType, HttpRequestDefaults } from './types.js';
|
|
7
|
-
declare const kRequest: unique symbol;
|
|
8
6
|
export declare namespace HttpRequestObservable {
|
|
9
7
|
interface Options {
|
|
10
8
|
observe?: HttpObserveType;
|
|
@@ -12,17 +10,14 @@ export declare namespace HttpRequestObservable {
|
|
|
12
10
|
}
|
|
13
11
|
}
|
|
14
12
|
export declare class HttpRequestObservable<T, TBody, TResponseExt = {}> extends Observable<T> {
|
|
15
|
-
static kContext: symbol;
|
|
16
|
-
static kRequest: symbol;
|
|
17
13
|
readonly contentId: string;
|
|
18
|
-
protected [
|
|
14
|
+
protected [kContext]: HttpClientContext;
|
|
19
15
|
protected [kRequest]: HttpRequest;
|
|
20
16
|
constructor(context: HttpClientContext, options?: HttpRequestObservable.Options);
|
|
21
|
-
header
|
|
17
|
+
header(name: string, value: string | string[]): this;
|
|
22
18
|
param(name: string, value: any): this;
|
|
23
19
|
fetch(): Promise<TBody>;
|
|
24
20
|
fetch(observe: HttpObserveType.Body): Promise<TBody>;
|
|
25
21
|
fetch(observe: HttpObserveType.Response): Promise<HttpResponse<TBody> & TResponseExt>;
|
|
26
22
|
with(cb: (_this: this) => void): this;
|
|
27
23
|
}
|
|
28
|
-
export {};
|
package/types/http-request.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
|
-
import {
|
|
2
|
+
import { OpraURL } from '@opra/common';
|
|
3
|
+
import { URLSearchParamsInit } from './types.js';
|
|
3
4
|
export declare namespace HttpRequest {
|
|
4
5
|
interface Initiator {
|
|
5
6
|
cache?: RequestCache;
|
|
@@ -10,7 +11,7 @@ export declare namespace HttpRequest {
|
|
|
10
11
|
keepalive?: boolean;
|
|
11
12
|
method?: string;
|
|
12
13
|
mode?: RequestMode;
|
|
13
|
-
params?:
|
|
14
|
+
params?: URLSearchParamsInit;
|
|
14
15
|
redirect?: RequestRedirect;
|
|
15
16
|
referrer?: string;
|
|
16
17
|
referrerPolicy?: ReferrerPolicy;
|
|
@@ -58,18 +59,16 @@ export declare class HttpRequest {
|
|
|
58
59
|
* whether or not request has been aborted, and its abort event handler. */
|
|
59
60
|
signal?: AbortSignal;
|
|
60
61
|
/** Returns the parsed url as OpraURL instance */
|
|
61
|
-
|
|
62
|
+
parsedUrl: OpraURL;
|
|
62
63
|
/** Body of the http request */
|
|
63
64
|
body?: any;
|
|
64
65
|
duplex?: 'half';
|
|
65
66
|
constructor(init?: HttpRequest.Initiator);
|
|
66
67
|
/** Returns the URL of request as a string. */
|
|
67
|
-
get url():
|
|
68
|
-
set url(value:
|
|
68
|
+
get url(): OpraURL;
|
|
69
|
+
set url(value: OpraURL);
|
|
69
70
|
/** Returns the searchParams of the URL as OpraURLSearchParams */
|
|
70
|
-
get params():
|
|
71
|
-
/** Returns the path part of URL as OpraURLPath */
|
|
72
|
-
get path(): OpraURLPath;
|
|
71
|
+
get params(): URLSearchParams;
|
|
73
72
|
clone(...update: (HttpRequest | HttpRequest.Initiator)[]): HttpRequest;
|
|
74
73
|
merge(update: HttpRequest | HttpRequest.Initiator): void;
|
|
75
74
|
inset(src: HttpRequest | HttpRequest.Initiator): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StrictOmit } from 'ts-gems';
|
|
2
2
|
import { PartialInput } from '@opra/common';
|
|
3
|
-
import {
|
|
3
|
+
import { kContext } from './constants.js';
|
|
4
4
|
import { HttpRequestObservable } from './http-request-observable.js';
|
|
5
5
|
import { HttpResponse } from './http-response.js';
|
|
6
6
|
import { HttpClientContext, HttpEvent, HttpObserveType } from './types.js';
|
|
@@ -24,7 +24,7 @@ export declare namespace HttpSingletonNode {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
export declare class HttpSingletonNode<TType, TResponseExt = {}> {
|
|
27
|
-
protected [
|
|
27
|
+
protected [kContext]: HttpClientContext;
|
|
28
28
|
constructor(context: HttpClientContext);
|
|
29
29
|
create(data: PartialInput<TType>, options?: StrictOmit<HttpSingletonNode.CreateOptions, 'observe'> & {
|
|
30
30
|
observe?: HttpObserveType.Body;
|
package/types/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Observable } from 'rxjs';
|
|
2
|
-
import type {
|
|
2
|
+
import type { OpraSchema } from '@opra/common';
|
|
3
3
|
import type { OpraHttpClient } from './client';
|
|
4
4
|
import type { HttpRequest } from './http-request.js';
|
|
5
5
|
import type { HttpResponse } from './http-response.js';
|
|
@@ -7,6 +7,7 @@ export type HttpRequestHandler = (observe: HttpObserveType, request: HttpRequest
|
|
|
7
7
|
export type RequestInterceptor = (ctx: HttpClientContext, request: HttpRequest) => void | Promise<void>;
|
|
8
8
|
export type ResponseInterceptor = ((ctx: HttpClientContext, observe: HttpObserveType, response: any) => void | Promise<void>);
|
|
9
9
|
export type HttpEvent = HttpSentEvent | HttpDownloadProgressEvent | HttpUploadProgressEvent | HttpResponseHeaderEvent | HttpResponseEvent | HttpUserEvent;
|
|
10
|
+
export type URLSearchParamsInit = string[][] | Record<string, string> | string | URLSearchParams;
|
|
10
11
|
export declare enum HttpObserveType {
|
|
11
12
|
Response = "response",
|
|
12
13
|
Body = "body",
|
|
@@ -44,15 +45,17 @@ export declare enum HttpEventType {
|
|
|
44
45
|
Custom = "custom"
|
|
45
46
|
}
|
|
46
47
|
export interface HttpClientContext {
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
client: OpraHttpClient;
|
|
49
|
+
resourceKind: OpraSchema.Resource.Kind;
|
|
50
|
+
resourceName: string;
|
|
51
|
+
operation: string;
|
|
49
52
|
send: HttpRequestHandler;
|
|
50
53
|
requestInterceptors?: RequestInterceptor[];
|
|
51
54
|
responseInterceptors?: ResponseInterceptor[];
|
|
52
55
|
}
|
|
53
56
|
export interface HttpRequestDefaults extends Partial<Pick<HttpRequest, 'cache' | 'credentials' | 'destination' | 'integrity' | 'keepalive' | 'mode' | 'redirect' | 'referrer' | 'referrerPolicy'>> {
|
|
54
57
|
headers?: HeadersInit;
|
|
55
|
-
params?:
|
|
58
|
+
params?: URLSearchParamsInit;
|
|
56
59
|
}
|
|
57
60
|
interface HttpEventBase {
|
|
58
61
|
observe: HttpObserveType;
|