@opra/angular 1.20.0 → 1.22.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.
@@ -1,246 +0,0 @@
1
- // @ts-ignore
2
- import { HttpContext, HttpHeaders, HttpParams } from '@angular/common/http';
3
- // @ts-ignore
4
- import * as Angular from '@angular/common/http';
5
- import typeIs from '@browsery/type-is';
6
- import {
7
- HttpBackend,
8
- type HttpDownloadProgressEvent,
9
- type HttpEvent,
10
- HttpEventType,
11
- HttpResponse,
12
- type HttpResponseEvent,
13
- type HttpResponseHeaderEvent,
14
- type HttpSentEvent,
15
- type HttpUploadProgressEvent,
16
- } from '@opra/client';
17
- import { isBlob } from '@opra/common';
18
- import { Observable } from 'rxjs';
19
- import { isReadableStreamLike } from 'rxjs/internal/util/isReadableStreamLike';
20
- import type { StrictOmit } from 'ts-gems';
21
-
22
- /**
23
- *
24
- * @class AngularBackend
25
- */
26
- export class AngularBackend extends HttpBackend {
27
- defaults: AngularBackend.RequestDefaults;
28
-
29
- constructor(
30
- readonly httpClient: Angular.HttpClient,
31
- serviceUrl: string,
32
- options?: AngularBackend.Options,
33
- ) {
34
- super(serviceUrl, options);
35
- this.defaults = {
36
- ...options?.defaults,
37
- headers:
38
- options?.defaults?.headers instanceof Headers
39
- ? options?.defaults?.headers
40
- : new Headers(options?.defaults?.headers),
41
- params:
42
- options?.defaults?.params instanceof URLSearchParams
43
- ? options?.defaults?.params
44
- : new URLSearchParams(options?.defaults?.params),
45
- };
46
- }
47
-
48
- handle(init: AngularBackend.RequestInit): Observable<HttpEvent> {
49
- const requestInit = this.prepareRequest(init);
50
- const request = new Angular.HttpRequest(
51
- requestInit.method,
52
- requestInit.url.toString(),
53
- {
54
- ...requestInit,
55
- headers: new HttpHeaders(requestInit.headers),
56
- },
57
- );
58
-
59
- const _this = this;
60
- return new Observable<HttpEvent>(subscriber => {
61
- // Send request
62
- this.send(request).subscribe({
63
- next(event) {
64
- if (event.type === Angular.HttpEventType.Sent) {
65
- // Emit 'Sent' event
66
- subscriber.next({
67
- type: HttpEventType.Sent,
68
- request,
69
- } satisfies HttpSentEvent);
70
- return;
71
- }
72
-
73
- if (event.type === Angular.HttpEventType.ResponseHeader) {
74
- // Emit 'ResponseHeader' event
75
- const headersResponse = _this.createResponse({
76
- url: request.url,
77
- headers: requestInit.headers,
78
- status: event.status,
79
- statusText: event.statusText,
80
- hasBody:
81
- event.headers.has('Content-Type') ||
82
- event.headers.has('Content-Length'),
83
- }) as HttpResponse<never>;
84
- subscriber.next({
85
- request,
86
- type: HttpEventType.ResponseHeader,
87
- response: headersResponse,
88
- } satisfies HttpResponseHeaderEvent);
89
- return;
90
- }
91
-
92
- if (event.type === Angular.HttpEventType.DownloadProgress) {
93
- // Emit 'DownloadProgress' event
94
- subscriber.next({
95
- request,
96
- type: HttpEventType.DownloadProgress,
97
- loaded: event.loaded,
98
- total: event.total,
99
- } satisfies HttpDownloadProgressEvent);
100
- }
101
-
102
- if (event.type === Angular.HttpEventType.UploadProgress) {
103
- // Emit 'UploadProgress' event
104
- subscriber.next({
105
- request,
106
- type: HttpEventType.UploadProgress,
107
- loaded: event.loaded,
108
- total: event.total,
109
- } satisfies HttpUploadProgressEvent);
110
- }
111
-
112
- if (event.type === Angular.HttpEventType.Response) {
113
- const headers = new Headers();
114
- event.headers
115
- .keys()
116
- .forEach(k => headers.set(k, event.headers.get(k) || ''));
117
- const response = _this.createResponse({
118
- url: request.url,
119
- headers,
120
- status: event.status,
121
- statusText: event.statusText,
122
- hasBody: !!event.body,
123
- body: event.body,
124
- });
125
- // Emit 'Response' event
126
- subscriber.next({
127
- type: HttpEventType.Response,
128
- request,
129
- response,
130
- } satisfies HttpResponseEvent);
131
- }
132
- },
133
- error(error) {
134
- subscriber.error(error);
135
- },
136
- complete() {
137
- subscriber.complete();
138
- },
139
- });
140
- });
141
- }
142
-
143
- protected send(request: Angular.HttpRequest<any>) {
144
- return this.httpClient.request(request);
145
- }
146
-
147
- protected prepareRequest(
148
- init: AngularBackend.RequestInit,
149
- ): AngularBackend.RequestInit {
150
- const headers = init.headers || new Headers();
151
- const requestInit: AngularBackend.RequestInit = {
152
- ...init,
153
- headers,
154
- };
155
- this.defaults.headers.forEach((val, key) => {
156
- if (!headers.has(key)) headers.set(key, val);
157
- });
158
- const url = new URL(requestInit.url, this.serviceUrl);
159
- if (this.defaults.params.size) {
160
- this.defaults.params.forEach((val, key) => {
161
- if (!url.searchParams.has(key)) url.searchParams.set(key, val);
162
- });
163
- requestInit.url = url.toString();
164
- }
165
- if (requestInit.body) {
166
- let body: any;
167
- let contentType: string;
168
- if (
169
- typeof requestInit.body === 'string' ||
170
- typeof requestInit.body === 'number' ||
171
- typeof requestInit.body === 'boolean'
172
- ) {
173
- contentType = 'text/plain; charset=UTF-8"';
174
- body = String(requestInit.body);
175
- headers.delete('Content-Size');
176
- } else if (isReadableStreamLike(requestInit.body)) {
177
- contentType = 'application/octet-stream';
178
- body = requestInit.body;
179
- } else if (Buffer.isBuffer(requestInit.body)) {
180
- contentType = 'application/octet-stream';
181
- body = requestInit.body;
182
- headers.set('Content-Size', String(requestInit.body.length));
183
- } else if (isBlob(requestInit.body)) {
184
- contentType = requestInit.body.type || 'application/octet-stream';
185
- body = requestInit.body;
186
- headers.set('Content-Size', String(requestInit.body.size));
187
- } else {
188
- contentType = 'application/json';
189
- body = JSON.stringify(requestInit.body);
190
- headers.delete('Content-Size');
191
- }
192
- if (!headers.has('Content-Type') && contentType)
193
- headers.set('Content-Type', contentType);
194
- requestInit.body = body;
195
- }
196
- return requestInit;
197
- }
198
-
199
- protected createResponse(init: HttpResponse.Initiator): HttpResponse {
200
- return new HttpResponse(init);
201
- }
202
-
203
- protected async parseBody(fetchResponse: Response): Promise<any> {
204
- let body: any;
205
- const contentType = fetchResponse.headers.get('Content-Type') || '';
206
- if (typeIs.is(contentType, ['json', 'application/*+json'])) {
207
- body = await fetchResponse.json();
208
- if (typeof body === 'string') body = JSON.parse(body);
209
- } else if (typeIs.is(contentType, ['text']))
210
- body = await fetchResponse.text();
211
- else if (typeIs.is(contentType, ['multipart']))
212
- body = await fetchResponse.formData();
213
- else {
214
- const buf = await fetchResponse.arrayBuffer();
215
- if (buf.byteLength) body = buf;
216
- }
217
- return body;
218
- }
219
- }
220
-
221
- /**
222
- * @namespace AngularBackend
223
- */
224
- export namespace AngularBackend {
225
- export interface Options extends HttpBackend.Options {
226
- defaults?: RequestDefaults;
227
- }
228
-
229
- export interface RequestInit extends HttpBackend.RequestInit {
230
- context?: HttpContext;
231
- reportProgress?: boolean;
232
- params?: HttpParams;
233
- responseType?: 'arraybuffer' | 'blob' | 'json' | 'text';
234
- withCredentials?: boolean;
235
- }
236
-
237
- export interface RequestOptions extends Pick<
238
- RequestInit,
239
- 'context' | 'reportProgress' | 'withCredentials'
240
- > {}
241
-
242
- export type RequestDefaults = StrictOmit<RequestOptions, 'context'> & {
243
- headers: Headers;
244
- params: URLSearchParams;
245
- };
246
- }
@@ -1,24 +0,0 @@
1
- // @ts-ignore
2
- import { HttpClient } from '@angular/common/http';
3
- import { HttpClientBase, kBackend } from '@opra/client';
4
- import { AngularBackend } from './angular-backend.js';
5
-
6
- /**
7
- *
8
- * @class OpraAngularClient
9
- */
10
- export class OpraAngularClient extends HttpClientBase<AngularBackend.RequestOptions> {
11
- declare [kBackend]: AngularBackend;
12
-
13
- constructor(
14
- httpClient: HttpClient,
15
- serviceUrl: string,
16
- options?: AngularBackend.Options,
17
- ) {
18
- super(new AngularBackend(httpClient, serviceUrl, options));
19
- }
20
-
21
- get defaults(): AngularBackend.RequestDefaults {
22
- return this[kBackend].defaults;
23
- }
24
- }
@@ -1,156 +0,0 @@
1
- /* eslint-disable import-x/extensions */
2
- // @ts-ignore
3
- import { HttpClient } from '@angular/common/http';
4
- import {
5
- type ModuleWithProviders,
6
- NgModule,
7
- type Provider,
8
- Type,
9
- } from '@angular/core';
10
- import { kClient } from '@opra/client';
11
- import { OpraAngularClient } from './angular-client';
12
- import { OPRA_CLIENT_MODULE_OPTIONS } from './constants';
13
- import type {
14
- OpraClientModuleAsyncOptions,
15
- OpraClientModuleOptions,
16
- } from './interfaces/module-options.interface';
17
-
18
- @NgModule({})
19
- export class OpraClientModule {
20
- public static registerClient(
21
- options: OpraClientModuleOptions,
22
- ): ModuleWithProviders<OpraClientModule> {
23
- const CLIENT_TOKEN = options.token || OpraAngularClient;
24
- return {
25
- ngModule: OpraClientModule,
26
- providers: [
27
- {
28
- provide: CLIENT_TOKEN,
29
- deps: [HttpClient],
30
- useFactory: (httpClient: HttpClient) =>
31
- new OpraAngularClient(httpClient, options.serviceUrl, options),
32
- },
33
- ],
34
- };
35
- }
36
-
37
- public static registerService<T>(
38
- serviceClass: Type<T>,
39
- options: OpraClientModuleOptions,
40
- ): ModuleWithProviders<OpraClientModule> {
41
- const SERVICE_TOKEN = options.token || serviceClass;
42
- return {
43
- ngModule: OpraClientModule,
44
- providers: [
45
- {
46
- provide: SERVICE_TOKEN,
47
- deps: [HttpClient],
48
- useFactory: (httpClient: HttpClient) => {
49
- const opraAngularClient = new OpraAngularClient(
50
- httpClient,
51
- options.serviceUrl,
52
- options,
53
- );
54
- const service = new serviceClass(opraAngularClient);
55
- service[kClient] = opraAngularClient;
56
- return service;
57
- },
58
- },
59
- ],
60
- };
61
- }
62
-
63
- public static registerClientAsync(
64
- options: OpraClientModuleAsyncOptions,
65
- ): ModuleWithProviders<OpraClientModule> {
66
- const CLIENT_TOKEN = options.token || OpraAngularClient;
67
- const asyncProviders = this._createAsyncProviders(options);
68
- return {
69
- ngModule: OpraClientModule,
70
- providers: [
71
- ...asyncProviders,
72
- {
73
- provide: CLIENT_TOKEN,
74
- deps: [HttpClient, OPRA_CLIENT_MODULE_OPTIONS],
75
- useFactory: (httpClient: HttpClient, opts: OpraClientModuleOptions) =>
76
- new OpraAngularClient(httpClient, opts.serviceUrl, opts),
77
- },
78
- ],
79
- };
80
- }
81
-
82
- public static registerServiceAsync<T>(
83
- serviceClass: Type<T>,
84
- options: OpraClientModuleAsyncOptions,
85
- ): ModuleWithProviders<OpraClientModule> {
86
- const SERVICE_TOKEN = options.token || serviceClass;
87
- const asyncProviders = this._createAsyncProviders(options);
88
- return {
89
- ngModule: OpraClientModule,
90
- providers: [
91
- ...asyncProviders,
92
- {
93
- provide: SERVICE_TOKEN,
94
- deps: [HttpClient, OPRA_CLIENT_MODULE_OPTIONS],
95
- useFactory: (
96
- httpClient: HttpClient,
97
- opts: OpraClientModuleOptions,
98
- ) => {
99
- const opraAngularClient = new OpraAngularClient(
100
- httpClient,
101
- opts.serviceUrl,
102
- opts,
103
- );
104
- const service = new serviceClass(opraAngularClient);
105
- service[kClient] = opraAngularClient;
106
- return service;
107
- },
108
- },
109
- ],
110
- };
111
- }
112
-
113
- private static _createAsyncProviders(
114
- options: OpraClientModuleAsyncOptions,
115
- ): Provider[] {
116
- if (options.useExisting || options.useFactory)
117
- return [this._createAsyncOptionsProvider(options)];
118
-
119
- if (options.useClass) {
120
- return [
121
- this._createAsyncOptionsProvider(options),
122
- {
123
- provide: options.useClass,
124
- useClass: options.useClass,
125
- },
126
- ];
127
- }
128
-
129
- throw new Error(
130
- 'Invalid configuration. Must provide useFactory, useClass or useExisting',
131
- );
132
- }
133
-
134
- private static _createAsyncOptionsProvider(
135
- options: OpraClientModuleAsyncOptions,
136
- ): Provider {
137
- if (options.useFactory) {
138
- return {
139
- provide: OPRA_CLIENT_MODULE_OPTIONS,
140
- useFactory: options.useFactory,
141
- deps: options.deps || [],
142
- };
143
- }
144
- const useClass = options.useClass || options.useExisting;
145
- if (useClass) {
146
- return {
147
- provide: OPRA_CLIENT_MODULE_OPTIONS,
148
- useFactory: o => o,
149
- deps: [useClass],
150
- };
151
- }
152
- throw new Error(
153
- 'Invalid configuration. Must provide useFactory, useClass or useExisting',
154
- );
155
- }
156
- }
package/src/constants.ts DELETED
@@ -1 +0,0 @@
1
- export const OPRA_CLIENT_MODULE_OPTIONS = 'OPRA_CLIENT_MODULE_OPTIONS';
@@ -1,27 +0,0 @@
1
- import { InjectionToken, NgModule, Type } from '@angular/core';
2
- import type { StrictOmit } from 'ts-gems';
3
- import type { AngularBackend } from '../angular-backend';
4
-
5
- export type OpraClientModuleOptions = AngularBackend.Options & {
6
- serviceUrl: string;
7
- token?: string | InjectionToken<any>;
8
- };
9
-
10
- type _OpraClientModuleOptions = StrictOmit<OpraClientModuleOptions, 'token'>;
11
-
12
- export interface OpraModuleOptionsFactory {
13
- createOptions(): Promise<_OpraClientModuleOptions> | _OpraClientModuleOptions;
14
- }
15
-
16
- export interface OpraClientModuleAsyncOptions extends Pick<
17
- NgModule,
18
- 'imports' | 'providers'
19
- > {
20
- token?: string | InjectionToken<any>;
21
- useExisting?: Type<OpraModuleOptionsFactory>;
22
- useClass?: Type<any>;
23
- useFactory?: (
24
- ...args: any[]
25
- ) => Promise<_OpraClientModuleOptions> | _OpraClientModuleOptions;
26
- deps?: any[];
27
- }
package/src/public_api.ts DELETED
@@ -1,5 +0,0 @@
1
- /* eslint-disable import-x/extensions */
2
- export * from './angular-backend';
3
- export * from './client.module';
4
- export * from './constants';
5
- export * from './interfaces/module-options.interface';
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "extends": "../../tsconfig-base.json",
3
- "include": ["src"],
4
- "compilerOptions": {
5
- "baseUrl": "./",
6
- "paths": {
7
- "@opra/client": ["../client/src"],
8
- "@opra/common": ["../common/src"]
9
- }
10
- }
11
- }
package/tsconfig.lib.json DELETED
@@ -1,34 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "target": "ES2020",
5
- "module": "ES2020",
6
- "moduleResolution": "node",
7
- "rootDir": "src",
8
- "baseUrl": "./",
9
- "paths": {
10
- "@opra/client": ["../client/build"],
11
- "@opra/common": ["../common/build"]
12
- },
13
- "resolveJsonModule": true,
14
- "esModuleInterop": true,
15
- "allowSyntheticDefaultImports": true,
16
- "declaration": true,
17
- "inlineSourceMap": true,
18
- "inlineSources": true,
19
- "skipLibCheck": true,
20
- "emitDecoratorMetadata": false,
21
- "experimentalDecorators": true,
22
- "importHelpers": true,
23
- "noEmit": false,
24
- "lib": ["dom", "es2018"]
25
- },
26
- "include": ["src"],
27
- "exclude": ["node_modules", "dist", "**/*.shim.ts", "**/*.spec.ts"],
28
- "angularCompilerOptions": {
29
- "compilationMode": "partial",
30
- "strictTemplates": true,
31
- "enableResourceInlining": true,
32
- "incremental": true
33
- }
34
- }
File without changes
File without changes