@leancodepl/rx-cqrs-client 7.1.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/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.cjs.js ADDED
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var rxjs = require('rxjs');
6
+ var operators = require('rxjs/operators');
7
+ var validation = require('@leancodepl/validation');
8
+ var ajax = require('rxjs/ajax');
9
+
10
+ function handleCommandResponse(handlerFunc) {
11
+ return (source)=>source.pipe(operators.concatMap((handler)=>{
12
+ const subj = new rxjs.ReplaySubject();
13
+ handlerFunc(handler)({
14
+ reducer: (prev, cur)=>subj.next(cur),
15
+ initialValue: null
16
+ });
17
+ subj.complete();
18
+ return subj;
19
+ }));
20
+ }
21
+
22
+ function authGuard(tokenProvider) {
23
+ return (response)=>response.pipe(operators.retryWhen((errors$)=>errors$.pipe(operators.mergeMap((error, i)=>{
24
+ if (i === 0 && error.status === 401) {
25
+ return rxjs.from(tokenProvider.invalidateToken()).pipe(operators.mergeMap((success)=>success ? rxjs.of(success) : rxjs.throwError(()=>error)));
26
+ }
27
+ return rxjs.throwError(()=>error);
28
+ }))));
29
+ }
30
+
31
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, ajaxOptions }) {
32
+ return {
33
+ createQuery (type) {
34
+ const queryCall = (dto, token)=>ajax.ajax({
35
+ ...ajaxOptions,
36
+ headers: {
37
+ Authorization: token,
38
+ "Content-Type": "application/json"
39
+ },
40
+ url: `${cqrsEndpoint}/query/${type}`,
41
+ method: "POST",
42
+ responseType: "json",
43
+ body: dto
44
+ });
45
+ if (tokenProvider) {
46
+ return (dto)=>rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>queryCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
47
+ }
48
+ return (dto)=>queryCall(dto).pipe(operators.map(({ response })=>response));
49
+ },
50
+ createOperation (type) {
51
+ const operationCall = (dto, token)=>ajax.ajax({
52
+ ...ajaxOptions,
53
+ headers: {
54
+ Authorization: token,
55
+ "Content-Type": "application/json"
56
+ },
57
+ url: `${cqrsEndpoint}/operation/${type}`,
58
+ method: "POST",
59
+ responseType: "json",
60
+ body: dto
61
+ });
62
+ if (tokenProvider) {
63
+ return (dto)=>rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>operationCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
64
+ }
65
+ return (dto)=>operationCall(dto).pipe(operators.map(({ response })=>response));
66
+ },
67
+ createCommand (type, errorCodesMap) {
68
+ const commandCall = (dto, token)=>ajax.ajax({
69
+ ...ajaxOptions,
70
+ headers: {
71
+ Authorization: token,
72
+ "Content-Type": "application/json"
73
+ },
74
+ url: `${cqrsEndpoint}/command/${type}`,
75
+ method: "POST",
76
+ responseType: "json",
77
+ body: dto
78
+ });
79
+ function call(dto) {
80
+ if (tokenProvider) {
81
+ return rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>commandCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
82
+ }
83
+ return commandCall(dto).pipe(operators.map(({ response })=>response));
84
+ }
85
+ call.handle = (dto)=>call(dto).pipe(operators.map((result)=>({
86
+ isSuccess: true,
87
+ result
88
+ })), rxjs.catchError((e)=>{
89
+ if (e instanceof ajax.AjaxError && e.status === 422) {
90
+ return rxjs.of({
91
+ isSuccess: true,
92
+ result: e.response
93
+ });
94
+ }
95
+ return rxjs.of({
96
+ isSuccess: false,
97
+ error: e
98
+ });
99
+ }), operators.map((response)=>validation.handleResponse(response, errorCodesMap)));
100
+ return call;
101
+ }
102
+ };
103
+ }
104
+
105
+ function reduceBoolean() {
106
+ return (source)=>source.pipe(rxjs.reduce((prev, cur)=>prev && cur, true));
107
+ }
108
+
109
+ function reduceObject() {
110
+ return (source)=>source.pipe(rxjs.reduce((prev, cur)=>({
111
+ ...prev,
112
+ ...cur
113
+ }), {}));
114
+ }
115
+
116
+ exports.handleCommandResponse = handleCommandResponse;
117
+ exports.mkCqrsClient = mkCqrsClient;
118
+ exports.reduceBoolean = reduceBoolean;
119
+ exports.reduceObject = reduceObject;
package/index.esm.js ADDED
@@ -0,0 +1,112 @@
1
+ import { ReplaySubject, from, of, throwError, catchError, reduce } from 'rxjs';
2
+ import { concatMap, retryWhen, mergeMap, map } from 'rxjs/operators';
3
+ import { handleResponse } from '@leancodepl/validation';
4
+ import { AjaxError, ajax } from 'rxjs/ajax';
5
+
6
+ function handleCommandResponse(handlerFunc) {
7
+ return (source)=>source.pipe(concatMap((handler)=>{
8
+ const subj = new ReplaySubject();
9
+ handlerFunc(handler)({
10
+ reducer: (prev, cur)=>subj.next(cur),
11
+ initialValue: null
12
+ });
13
+ subj.complete();
14
+ return subj;
15
+ }));
16
+ }
17
+
18
+ function authGuard(tokenProvider) {
19
+ return (response)=>response.pipe(retryWhen((errors$)=>errors$.pipe(mergeMap((error, i)=>{
20
+ if (i === 0 && error.status === 401) {
21
+ return from(tokenProvider.invalidateToken()).pipe(mergeMap((success)=>success ? of(success) : throwError(()=>error)));
22
+ }
23
+ return throwError(()=>error);
24
+ }))));
25
+ }
26
+
27
+ function mkCqrsClient({ cqrsEndpoint, tokenProvider, ajaxOptions }) {
28
+ return {
29
+ createQuery (type) {
30
+ const queryCall = (dto, token)=>ajax({
31
+ ...ajaxOptions,
32
+ headers: {
33
+ Authorization: token,
34
+ "Content-Type": "application/json"
35
+ },
36
+ url: `${cqrsEndpoint}/query/${type}`,
37
+ method: "POST",
38
+ responseType: "json",
39
+ body: dto
40
+ });
41
+ if (tokenProvider) {
42
+ return (dto)=>from(tokenProvider.getToken()).pipe(mergeMap((token)=>queryCall(dto, token).pipe(authGuard(tokenProvider))), map(({ response })=>response));
43
+ }
44
+ return (dto)=>queryCall(dto).pipe(map(({ response })=>response));
45
+ },
46
+ createOperation (type) {
47
+ const operationCall = (dto, token)=>ajax({
48
+ ...ajaxOptions,
49
+ headers: {
50
+ Authorization: token,
51
+ "Content-Type": "application/json"
52
+ },
53
+ url: `${cqrsEndpoint}/operation/${type}`,
54
+ method: "POST",
55
+ responseType: "json",
56
+ body: dto
57
+ });
58
+ if (tokenProvider) {
59
+ return (dto)=>from(tokenProvider.getToken()).pipe(mergeMap((token)=>operationCall(dto, token).pipe(authGuard(tokenProvider))), map(({ response })=>response));
60
+ }
61
+ return (dto)=>operationCall(dto).pipe(map(({ response })=>response));
62
+ },
63
+ createCommand (type, errorCodesMap) {
64
+ const commandCall = (dto, token)=>ajax({
65
+ ...ajaxOptions,
66
+ headers: {
67
+ Authorization: token,
68
+ "Content-Type": "application/json"
69
+ },
70
+ url: `${cqrsEndpoint}/command/${type}`,
71
+ method: "POST",
72
+ responseType: "json",
73
+ body: dto
74
+ });
75
+ function call(dto) {
76
+ if (tokenProvider) {
77
+ return from(tokenProvider.getToken()).pipe(mergeMap((token)=>commandCall(dto, token).pipe(authGuard(tokenProvider))), map(({ response })=>response));
78
+ }
79
+ return commandCall(dto).pipe(map(({ response })=>response));
80
+ }
81
+ call.handle = (dto)=>call(dto).pipe(map((result)=>({
82
+ isSuccess: true,
83
+ result
84
+ })), catchError((e)=>{
85
+ if (e instanceof AjaxError && e.status === 422) {
86
+ return of({
87
+ isSuccess: true,
88
+ result: e.response
89
+ });
90
+ }
91
+ return of({
92
+ isSuccess: false,
93
+ error: e
94
+ });
95
+ }), map((response)=>handleResponse(response, errorCodesMap)));
96
+ return call;
97
+ }
98
+ };
99
+ }
100
+
101
+ function reduceBoolean() {
102
+ return (source)=>source.pipe(reduce((prev, cur)=>prev && cur, true));
103
+ }
104
+
105
+ function reduceObject() {
106
+ return (source)=>source.pipe(reduce((prev, cur)=>({
107
+ ...prev,
108
+ ...cur
109
+ }), {}));
110
+ }
111
+
112
+ export { handleCommandResponse, mkCqrsClient, reduceBoolean, reduceObject };
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@leancodepl/rx-cqrs-client",
3
+ "version": "7.1.1",
4
+ "dependencies": {
5
+ "@leancodepl/validation": "7.1.0",
6
+ "rxjs": ">=7.0.0"
7
+ },
8
+ "type": "commonjs",
9
+ "main": "./index.cjs.js",
10
+ "module": "./index.esm.js"
11
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { handleCommandResponse } from "./lib/handleCommandResponse";
2
+ export { mkCqrsClient } from "./lib/mkCqrsClient";
3
+ export { reduceBoolean } from "./lib/reduceBoolean";
4
+ export { reduceObject } from "./lib/reduceObject";
@@ -0,0 +1,3 @@
1
+ import { TokenProvider } from "@leancodepl/cqrs-client-base";
2
+ import { MonoTypeOperatorFunction } from "rxjs";
3
+ export default function authGuard<T>(tokenProvider: TokenProvider): MonoTypeOperatorFunction<T>;
@@ -0,0 +1,3 @@
1
+ import type { ReducerDescription, ValidationErrorsHandler } from "@leancodepl/validation";
2
+ import { OperatorFunction } from "rxjs";
3
+ export declare function handleCommandResponse<TErrorCodes extends Record<string, number>, THandlerResult>(handlerFunc: (handler: ValidationErrorsHandler<TErrorCodes, never>) => (reducer: ReducerDescription<THandlerResult, any>) => any): OperatorFunction<ValidationErrorsHandler<TErrorCodes, never>, THandlerResult>;
@@ -0,0 +1,19 @@
1
+ import { CommandResult, TokenProvider } from "@leancodepl/cqrs-client-base";
2
+ import { AjaxConfig } from "rxjs/ajax";
3
+ export declare function mkCqrsClient({ cqrsEndpoint, tokenProvider, ajaxOptions, }: {
4
+ cqrsEndpoint: string;
5
+ tokenProvider?: TokenProvider;
6
+ ajaxOptions?: Omit<AjaxConfig, "headers" | "url" | "method" | "responseType" | "body">;
7
+ }): {
8
+ createQuery<TQuery, TResult>(type: string): (dto: TQuery) => import("rxjs").Observable<TResult>;
9
+ createOperation<TOperation, TResult_1>(type: string): (dto: TOperation) => import("rxjs").Observable<TResult_1>;
10
+ createCommand<TCommand, TErrorCodes extends {
11
+ [name: string]: number;
12
+ }>(type: string, errorCodesMap: TErrorCodes): {
13
+ (dto: TCommand): import("rxjs").Observable<CommandResult<TErrorCodes>>;
14
+ handle(dto: TCommand): import("rxjs").Observable<import("@leancodepl/validation").ValidationErrorsHandler<TErrorCodes & {
15
+ readonly success: -1;
16
+ readonly failure: -2;
17
+ }, never>>;
18
+ };
19
+ };
@@ -0,0 +1,2 @@
1
+ import { OperatorFunction } from "rxjs";
2
+ export declare function reduceBoolean(): OperatorFunction<boolean, boolean>;
@@ -0,0 +1,4 @@
1
+ import { OperatorFunction } from "rxjs";
2
+ type UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any ? R : never;
3
+ export declare function reduceObject<T>(): OperatorFunction<T, Partial<UnionToIntersection<T>>>;
4
+ export {};