@opra/client 1.26.2 → 1.26.4

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/README.md CHANGED
@@ -1,3 +1,26 @@
1
1
  # @opra/client
2
2
 
3
- OPRA Client
3
+ [![NPM Version][npm-image]][npm-url]
4
+ [![NPM Downloads][downloads-image]][downloads-url]
5
+ [![CI Tests][ci-test-image]][ci-test-url]
6
+ [![Test Coverage][coveralls-image]][coveralls-url]
7
+
8
+
9
+ ## Support
10
+ You can report bugs and discuss features on the [GitHub issues](https://github.com/panates/opra/issues) page.
11
+
12
+ ## Node Compatibility
13
+ - node >= 20.x
14
+
15
+
16
+ ## License
17
+ Available under [MIT](LICENSE) license.
18
+
19
+ [npm-image]: https://img.shields.io/npm/v/@opra/client
20
+ [npm-url]: https://npmjs.org/package/@opra/client
21
+ [downloads-image]: https://img.shields.io/npm/dm/@opra/client.svg
22
+ [downloads-url]: https://npmjs.org/package/@opra/client
23
+ [ci-test-image]: https://github.com/panates/opra/actions/workflows/test.yml/badge.svg
24
+ [ci-test-url]: https://github.com/panates/opra/actions/workflows/test.yml
25
+ [coveralls-image]: https://coveralls.io/repos/github/panates/opra/badge.svg?branch=main
26
+ [coveralls-url]: https://coveralls.io/github/panates/opra?branch=main
package/core/backend.d.ts CHANGED
@@ -1,18 +1,41 @@
1
1
  import { ApiDocument } from '@opra/common';
2
2
  import { Observable } from 'rxjs';
3
3
  import type { HttpEvent } from '../http/interfaces/http-event.js';
4
+ /**
5
+ * Abstract base class for all backends.
6
+ *
7
+ * @class Backend
8
+ */
4
9
  export declare abstract class Backend implements Backend {
10
+ /** The API document associated with this backend */
5
11
  document?: ApiDocument;
12
+ /**
13
+ * Creates a new instance of Backend.
14
+ *
15
+ * @param options Configuration options.
16
+ * @protected
17
+ */
6
18
  protected constructor(options?: Backend.Options);
19
+ /**
20
+ * Handles the request and returns an observable of {@link HttpEvent}.
21
+ *
22
+ * @param init The request initialization parameters.
23
+ * @returns An observable of HttpEvent.
24
+ */
7
25
  abstract handle(init: Backend.RequestInit): Observable<HttpEvent>;
8
26
  }
9
27
  /**
28
+ * Namespace for {@link Backend} related types and interfaces.
29
+ *
10
30
  * @namespace Backend
11
31
  */
12
32
  export declare namespace Backend {
33
+ /** Configuration options for Backend */
13
34
  interface Options {
35
+ /** The API document associated with this backend */
14
36
  document?: ApiDocument;
15
37
  }
38
+ /** Request initialization parameters for Backend */
16
39
  interface RequestInit {
17
40
  }
18
41
  }
package/core/backend.js CHANGED
@@ -1,5 +1,17 @@
1
+ /**
2
+ * Abstract base class for all backends.
3
+ *
4
+ * @class Backend
5
+ */
1
6
  export class Backend {
7
+ /** The API document associated with this backend */
2
8
  document;
9
+ /**
10
+ * Creates a new instance of Backend.
11
+ *
12
+ * @param options Configuration options.
13
+ * @protected
14
+ */
3
15
  constructor(options) {
4
16
  this.document = options?.document;
5
17
  }
@@ -1,8 +1,21 @@
1
1
  import type { ErrorIssue } from '@opra/common';
2
+ /**
3
+ * Error class for OPRA client operations.
4
+ *
5
+ * @class ClientError
6
+ */
2
7
  export declare class ClientError extends Error {
3
8
  cause?: Error | undefined;
9
+ /** Detailed error issues */
4
10
  issues: ErrorIssue[];
11
+ /** HTTP status code */
5
12
  status?: number;
13
+ /**
14
+ * Creates a new instance of ClientError.
15
+ *
16
+ * @param init Initialization parameters.
17
+ * @param cause The underlying error cause.
18
+ */
6
19
  constructor(init: {
7
20
  message: string;
8
21
  issues?: ErrorIssue[];
@@ -1,7 +1,20 @@
1
+ /**
2
+ * Error class for OPRA client operations.
3
+ *
4
+ * @class ClientError
5
+ */
1
6
  export class ClientError extends Error {
2
7
  cause;
8
+ /** Detailed error issues */
3
9
  issues;
10
+ /** HTTP status code */
4
11
  status;
12
+ /**
13
+ * Creates a new instance of ClientError.
14
+ *
15
+ * @param init Initialization parameters.
16
+ * @param cause The underlying error cause.
17
+ */
5
18
  constructor(init, cause) {
6
19
  super(init.message);
7
20
  this.cause = cause;
@@ -2,25 +2,42 @@ import { Backend } from '../core/backend.js';
2
2
  import type { HttpHandler } from './interfaces/http-handler.js';
3
3
  import type { HttpInterceptor } from './interfaces/http-interceptor.js';
4
4
  /**
5
+ * Base class for HTTP backends.
5
6
  *
6
7
  * @class HttpBackend
7
8
  */
8
9
  export declare abstract class HttpBackend extends Backend implements HttpHandler {
10
+ /** The base URL of the service */
9
11
  readonly serviceUrl: string;
12
+ /** List of HTTP interceptors */
10
13
  interceptors?: HttpInterceptor<any>[];
14
+ /**
15
+ * Creates a new instance of HttpBackend.
16
+ *
17
+ * @param serviceUrl The base URL of the service.
18
+ * @param options Configuration options.
19
+ * @protected
20
+ */
11
21
  protected constructor(serviceUrl: string, options?: HttpBackend.Options);
12
22
  }
13
23
  /**
24
+ * Namespace for {@link HttpBackend} related types and interfaces.
14
25
  *
15
26
  * @namespace HttpBackend
16
27
  */
17
28
  export declare namespace HttpBackend {
29
+ /** Configuration options for HttpBackend */
18
30
  interface Options extends Backend.Options {
19
31
  }
32
+ /** Request initialization parameters for HttpBackend */
20
33
  interface RequestInit extends Backend.RequestInit {
34
+ /** HTTP method (GET, POST, etc.) */
21
35
  method: string;
36
+ /** The target URL */
22
37
  url: string | URL;
38
+ /** HTTP headers */
23
39
  headers?: Headers;
40
+ /** Request body */
24
41
  body?: any;
25
42
  }
26
43
  }
@@ -1,11 +1,21 @@
1
1
  import { Backend } from '../core/backend.js';
2
2
  /**
3
+ * Base class for HTTP backends.
3
4
  *
4
5
  * @class HttpBackend
5
6
  */
6
7
  export class HttpBackend extends Backend {
8
+ /** The base URL of the service */
7
9
  serviceUrl;
10
+ /** List of HTTP interceptors */
8
11
  interceptors;
12
+ /**
13
+ * Creates a new instance of HttpBackend.
14
+ *
15
+ * @param serviceUrl The base URL of the service.
16
+ * @param options Configuration options.
17
+ * @protected
18
+ */
9
19
  constructor(serviceUrl, options) {
10
20
  super(options);
11
21
  const u = new URL(serviceUrl);
@@ -4,33 +4,100 @@ import { kBackend } from '../constants.js';
4
4
  import { HttpBackend } from './http-backend.js';
5
5
  import { HttpRequestObservable } from './http-request-observable.js';
6
6
  /**
7
+ * Namespace for {@link OpraClientBase} related types and interfaces.
7
8
  *
8
9
  * @namespace OpraClientBase
9
10
  */
10
11
  export declare namespace OpraClientBase {
12
+ /** Configuration options for OpraClientBase */
11
13
  interface Options {
14
+ /** The API document associated with this client */
12
15
  document?: ApiDocument;
13
16
  }
17
+ /** Generic request options for OPRA client */
14
18
  type RequestOptions = Partial<StrictOmit<HttpBackend.RequestInit, 'url'>> & {
19
+ /** URL parameters */
15
20
  params?: URLSearchParamsInit;
16
21
  };
17
22
  }
18
23
  /**
24
+ * Base class for OPRA HTTP clients.
19
25
  *
20
26
  * @class OpraClientBase
21
27
  * @abstract
22
28
  */
23
29
  export declare abstract class HttpClientBase<TRequestOptions = {}, TResponseExt = {}> {
24
30
  protected [kBackend]: HttpBackend;
31
+ /**
32
+ * Creates a new instance of HttpClientBase.
33
+ *
34
+ * @param backend The backend instance to use for requests.
35
+ * @protected
36
+ */
25
37
  protected constructor(backend: HttpBackend);
38
+ /**
39
+ * Gets the base service URL.
40
+ */
26
41
  get serviceUrl(): string;
42
+ /**
43
+ * Fetches the API document from the service.
44
+ *
45
+ * @param options Fetch options.
46
+ * @returns A promise that resolves to an ApiDocument.
47
+ * @throws {@link Error} if there is an issue fetching or parsing the document.
48
+ */
27
49
  fetchDocument(options?: {
28
50
  documentId?: string;
29
51
  }): Promise<ApiDocument>;
52
+ /**
53
+ * Creates a new {@link HttpRequestObservable} for a specific path.
54
+ *
55
+ * @param path The path of the request.
56
+ * @param options Request options.
57
+ * @returns A new HttpRequestObservable instance.
58
+ */
30
59
  request<TBody = any>(path: string, options?: OpraClientBase.RequestOptions): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
60
+ /**
61
+ * Sends a DELETE request.
62
+ *
63
+ * @param path The path of the request.
64
+ * @param options Request options.
65
+ * @returns A new HttpRequestObservable instance.
66
+ */
31
67
  delete<TBody = any>(path: string, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
68
+ /**
69
+ * Sends a GET request.
70
+ *
71
+ * @param path The path of the request.
72
+ * @param options Request options.
73
+ * @returns A new HttpRequestObservable instance.
74
+ */
32
75
  get<TBody = any>(path: string, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
76
+ /**
77
+ * Sends a PATCH request.
78
+ *
79
+ * @param path The path of the request.
80
+ * @param requestBody The request body.
81
+ * @param options Request options.
82
+ * @returns A new HttpRequestObservable instance.
83
+ */
33
84
  patch<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
85
+ /**
86
+ * Sends a POST request.
87
+ *
88
+ * @param path The path of the request.
89
+ * @param requestBody The request body.
90
+ * @param options Request options.
91
+ * @returns A new HttpRequestObservable instance.
92
+ */
34
93
  post<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
94
+ /**
95
+ * Sends a PUT request.
96
+ *
97
+ * @param path The path of the request.
98
+ * @param requestBody The request body.
99
+ * @param options Request options.
100
+ * @returns A new HttpRequestObservable instance.
101
+ */
35
102
  put<TBody = any>(path: string, requestBody: any, options?: StrictOmit<OpraClientBase.RequestOptions, 'method' | 'body'>): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>;
36
103
  }
@@ -4,20 +4,37 @@ import { kBackend } from '../constants.js';
4
4
  import { HttpRequestObservable } from './http-request-observable.js';
5
5
  const SPLIT_BACKSLASH_PATTERN = /^(\/*)(.+)/;
6
6
  /**
7
+ * Base class for OPRA HTTP clients.
7
8
  *
8
9
  * @class OpraClientBase
9
10
  * @abstract
10
11
  */
11
12
  export class HttpClientBase {
13
+ /**
14
+ * Creates a new instance of HttpClientBase.
15
+ *
16
+ * @param backend The backend instance to use for requests.
17
+ * @protected
18
+ */
12
19
  constructor(backend) {
13
20
  Object.defineProperty(this, kBackend, {
14
21
  enumerable: false,
15
22
  value: backend,
16
23
  });
17
24
  }
25
+ /**
26
+ * Gets the base service URL.
27
+ */
18
28
  get serviceUrl() {
19
29
  return this[kBackend].serviceUrl;
20
30
  }
31
+ /**
32
+ * Fetches the API document from the service.
33
+ *
34
+ * @param options Fetch options.
35
+ * @returns A promise that resolves to an ApiDocument.
36
+ * @throws {@link Error} if there is an issue fetching or parsing the document.
37
+ */
21
38
  async fetchDocument(options) {
22
39
  const documentMap = {};
23
40
  const getDocument = async (documentId) => {
@@ -52,8 +69,15 @@ export class HttpClientBase {
52
69
  throw e;
53
70
  });
54
71
  }
72
+ /**
73
+ * Creates a new {@link HttpRequestObservable} for a specific path.
74
+ *
75
+ * @param path The path of the request.
76
+ * @param options Request options.
77
+ * @returns A new HttpRequestObservable instance.
78
+ */
55
79
  request(path, options) {
56
- /** Remove leading backslashes */
80
+ /* Remove leading backslashes */
57
81
  path = SPLIT_BACKSLASH_PATTERN.exec(path)?.[2] || '';
58
82
  const observable = new HttpRequestObservable(this[kBackend], {
59
83
  ...options,
@@ -64,18 +88,40 @@ export class HttpClientBase {
64
88
  observable.param(options.params);
65
89
  return observable;
66
90
  }
91
+ /**
92
+ * Sends a DELETE request.
93
+ *
94
+ * @param path The path of the request.
95
+ * @param options Request options.
96
+ * @returns A new HttpRequestObservable instance.
97
+ */
67
98
  delete(path, options) {
68
99
  return this.request(path, {
69
100
  ...options,
70
101
  method: 'DELETE',
71
102
  });
72
103
  }
104
+ /**
105
+ * Sends a GET request.
106
+ *
107
+ * @param path The path of the request.
108
+ * @param options Request options.
109
+ * @returns A new HttpRequestObservable instance.
110
+ */
73
111
  get(path, options) {
74
112
  return this.request(path, {
75
113
  ...options,
76
114
  method: 'GET',
77
115
  });
78
116
  }
117
+ /**
118
+ * Sends a PATCH request.
119
+ *
120
+ * @param path The path of the request.
121
+ * @param requestBody The request body.
122
+ * @param options Request options.
123
+ * @returns A new HttpRequestObservable instance.
124
+ */
79
125
  patch(path, requestBody, options) {
80
126
  return this.request(path, {
81
127
  ...options,
@@ -83,6 +129,14 @@ export class HttpClientBase {
83
129
  body: requestBody,
84
130
  });
85
131
  }
132
+ /**
133
+ * Sends a POST request.
134
+ *
135
+ * @param path The path of the request.
136
+ * @param requestBody The request body.
137
+ * @param options Request options.
138
+ * @returns A new HttpRequestObservable instance.
139
+ */
86
140
  post(path, requestBody, options) {
87
141
  return this.request(path, {
88
142
  ...options,
@@ -90,6 +144,14 @@ export class HttpClientBase {
90
144
  body: requestBody,
91
145
  });
92
146
  }
147
+ /**
148
+ * Sends a PUT request.
149
+ *
150
+ * @param path The path of the request.
151
+ * @param requestBody The request body.
152
+ * @param options Request options.
153
+ * @returns A new HttpRequestObservable instance.
154
+ */
93
155
  put(path, requestBody, options) {
94
156
  return this.request(path, {
95
157
  ...options,
@@ -2,11 +2,21 @@ import { kBackend } from '../constants.js';
2
2
  import { FetchBackend } from './fetch-backend.js';
3
3
  import { HttpClientBase } from './http-client-base.js';
4
4
  /**
5
+ * Default implementation of {@link HttpClientBase} using {@link FetchBackend}.
5
6
  *
6
7
  * @class OpraHttpClient
7
8
  */
8
9
  export declare class OpraHttpClient extends HttpClientBase<FetchBackend.RequestOptions> {
9
10
  [kBackend]: FetchBackend;
11
+ /**
12
+ * Creates a new instance of OpraHttpClient.
13
+ *
14
+ * @param serviceUrl The base URL of the service.
15
+ * @param options Configuration options.
16
+ */
10
17
  constructor(serviceUrl: string, options?: FetchBackend.Options);
18
+ /**
19
+ * Gets the default request options from the backend.
20
+ */
11
21
  get defaults(): FetchBackend.RequestDefaults;
12
22
  }
@@ -2,13 +2,23 @@ import { kBackend } from '../constants.js';
2
2
  import { FetchBackend } from './fetch-backend.js';
3
3
  import { HttpClientBase } from './http-client-base.js';
4
4
  /**
5
+ * Default implementation of {@link HttpClientBase} using {@link FetchBackend}.
5
6
  *
6
7
  * @class OpraHttpClient
7
8
  */
8
9
  export class OpraHttpClient extends HttpClientBase {
10
+ /**
11
+ * Creates a new instance of OpraHttpClient.
12
+ *
13
+ * @param serviceUrl The base URL of the service.
14
+ * @param options Configuration options.
15
+ */
9
16
  constructor(serviceUrl, options) {
10
17
  super(new FetchBackend(serviceUrl, options));
11
18
  }
19
+ /**
20
+ * Gets the default request options from the backend.
21
+ */
12
22
  get defaults() {
13
23
  return this[kBackend].defaults;
14
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/client",
3
- "version": "1.26.2",
3
+ "version": "1.26.4",
4
4
  "description": "Opra Client package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -10,7 +10,7 @@
10
10
  "tslib": "^2.8.1"
11
11
  },
12
12
  "peerDependencies": {
13
- "@opra/common": "^1.26.2",
13
+ "@opra/common": "^1.26.4",
14
14
  "rxjs": "^7.0.0 || ^8.0.0"
15
15
  },
16
16
  "type": "module",