@leancodepl/rx-cqrs-client 8.4.0 → 8.5.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/package.json CHANGED
@@ -1,25 +1,59 @@
1
1
  {
2
2
  "name": "@leancodepl/rx-cqrs-client",
3
- "version": "8.4.0",
3
+ "version": "8.5.0",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/cqrs-client-base": "8.4.0",
7
- "@leancodepl/validation": "8.4.0",
6
+ "@leancodepl/cqrs-client-base": "8.5.0",
7
+ "@leancodepl/validation": "8.5.0",
8
8
  "rxjs": ">=7.0.0"
9
9
  },
10
10
  "devDependencies": {
11
11
  "xhr-mock": "2.5.1"
12
12
  },
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "registry": "https://registry.npmjs.org/"
16
+ },
17
+ "engines": {
18
+ "node": ">=18.0.0"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/leancodepl/js_corelibrary.git",
23
+ "directory": "packages/cqrs-clients/rx-cqrs-client"
24
+ },
25
+ "homepage": "https://github.com/leancodepl/js_corelibrary",
26
+ "bugs": {
27
+ "url": "https://github.com/leancodepl/js_corelibrary/issues"
28
+ },
29
+ "description": "RxJS-based reactive CQRS client for real-time applications",
30
+ "keywords": [
31
+ "cqrs",
32
+ "rxjs",
33
+ "typescript",
34
+ "javascript",
35
+ "leancode"
36
+ ],
37
+ "author": {
38
+ "name": "LeanCode",
39
+ "url": "https://leancode.co"
40
+ },
41
+ "files": [
42
+ "dist",
43
+ "README.md",
44
+ "CHANGELOG.md"
45
+ ],
46
+ "sideEffects": false,
13
47
  "exports": {
14
48
  "./package.json": "./package.json",
15
49
  ".": {
16
50
  "module": "./index.esm.js",
17
- "types": "./index.esm.d.ts",
51
+ "types": "./index.d.ts",
18
52
  "import": "./index.cjs.mjs",
19
53
  "default": "./index.cjs.js"
20
54
  }
21
55
  },
22
56
  "module": "./index.esm.js",
23
57
  "main": "./index.cjs.js",
24
- "types": "./index.esm.d.ts"
58
+ "types": "./index.d.ts"
25
59
  }
package/index.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
@@ -1 +0,0 @@
1
- exports._default = require('./index.cjs.js').default;
package/index.cjs.js DELETED
@@ -1,117 +0,0 @@
1
- 'use strict';
2
-
3
- var rxjs = require('rxjs');
4
- var operators = require('rxjs/operators');
5
- var ajax = require('rxjs/ajax');
6
- var validation = require('@leancodepl/validation');
7
-
8
- function handleCommandResponse(handlerFunc) {
9
- return (source)=>source.pipe(operators.concatMap((handler)=>{
10
- const subj = new rxjs.ReplaySubject();
11
- handlerFunc(handler)({
12
- reducer: (prev, cur)=>subj.next(cur),
13
- initialValue: null
14
- });
15
- subj.complete();
16
- return subj;
17
- }));
18
- }
19
-
20
- function authGuard(tokenProvider) {
21
- return (response)=>response.pipe(operators.retryWhen((errors$)=>errors$.pipe(operators.mergeMap((error, i)=>{
22
- if (i === 0 && error.status === 401) {
23
- return rxjs.from(tokenProvider.invalidateToken()).pipe(operators.mergeMap((success)=>success ? rxjs.of(success) : rxjs.throwError(()=>error)));
24
- }
25
- return rxjs.throwError(()=>error);
26
- }))));
27
- }
28
-
29
- function mkCqrsClient({ cqrsEndpoint, tokenProvider, ajaxOptions, tokenHeader = "Authorization" }) {
30
- return {
31
- createQuery (type) {
32
- const queryCall = (dto, token)=>ajax.ajax({
33
- ...ajaxOptions,
34
- headers: {
35
- [tokenHeader]: token,
36
- "Content-Type": "application/json"
37
- },
38
- url: `${cqrsEndpoint}/query/${type}`,
39
- method: "POST",
40
- responseType: "json",
41
- body: dto
42
- });
43
- if (tokenProvider) {
44
- return (dto)=>rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>queryCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
45
- }
46
- return (dto)=>queryCall(dto).pipe(operators.map(({ response })=>response));
47
- },
48
- createOperation (type) {
49
- const operationCall = (dto, token)=>ajax.ajax({
50
- ...ajaxOptions,
51
- headers: {
52
- Authorization: token,
53
- "Content-Type": "application/json"
54
- },
55
- url: `${cqrsEndpoint}/operation/${type}`,
56
- method: "POST",
57
- responseType: "json",
58
- body: dto
59
- });
60
- if (tokenProvider) {
61
- return (dto)=>rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>operationCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
62
- }
63
- return (dto)=>operationCall(dto).pipe(operators.map(({ response })=>response));
64
- },
65
- createCommand (type, errorCodesMap) {
66
- const commandCall = (dto, token)=>ajax.ajax({
67
- ...ajaxOptions,
68
- headers: {
69
- Authorization: token,
70
- "Content-Type": "application/json"
71
- },
72
- url: `${cqrsEndpoint}/command/${type}`,
73
- method: "POST",
74
- responseType: "json",
75
- body: dto
76
- });
77
- function call(dto) {
78
- if (tokenProvider) {
79
- return rxjs.from(tokenProvider.getToken()).pipe(operators.mergeMap((token)=>commandCall(dto, token).pipe(authGuard(tokenProvider))), operators.map(({ response })=>response));
80
- }
81
- return commandCall(dto).pipe(operators.map(({ response })=>response));
82
- }
83
- call.handle = (dto)=>call(dto).pipe(operators.map((result)=>({
84
- isSuccess: true,
85
- result
86
- })), rxjs.catchError((e)=>{
87
- if (e instanceof ajax.AjaxError && e.status === 422) {
88
- return rxjs.of({
89
- isSuccess: true,
90
- result: e.response
91
- });
92
- }
93
- return rxjs.of({
94
- isSuccess: false,
95
- error: e
96
- });
97
- }), operators.map((response)=>validation.handleResponse(response, errorCodesMap)));
98
- return call;
99
- }
100
- };
101
- }
102
-
103
- function reduceBoolean() {
104
- return (source)=>source.pipe(rxjs.reduce((prev, cur)=>prev && cur, true));
105
- }
106
-
107
- function reduceObject() {
108
- return (source)=>source.pipe(rxjs.reduce((prev, cur)=>({
109
- ...prev,
110
- ...cur
111
- }), {}));
112
- }
113
-
114
- exports.handleCommandResponse = handleCommandResponse;
115
- exports.mkCqrsClient = mkCqrsClient;
116
- exports.reduceBoolean = reduceBoolean;
117
- exports.reduceObject = reduceObject;
package/index.cjs.mjs DELETED
@@ -1,2 +0,0 @@
1
- export * from './index.cjs.js';
2
- export { _default as default } from './index.cjs.default.js';
package/index.esm.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
package/index.esm.js DELETED
@@ -1,112 +0,0 @@
1
- import { ReplaySubject, from, of, throwError, catchError, reduce } from 'rxjs';
2
- import { concatMap, retryWhen, mergeMap, map } from 'rxjs/operators';
3
- import { AjaxError, ajax } from 'rxjs/ajax';
4
- import { handleResponse } from '@leancodepl/validation';
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, tokenHeader = "Authorization" }) {
28
- return {
29
- createQuery (type) {
30
- const queryCall = (dto, token)=>ajax({
31
- ...ajaxOptions,
32
- headers: {
33
- [tokenHeader]: 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/src/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export { handleCommandResponse } from "./lib/handleCommandResponse";
2
- export { mkCqrsClient } from "./lib/mkCqrsClient";
3
- export { reduceBoolean } from "./lib/reduceBoolean";
4
- export { reduceObject } from "./lib/reduceObject";
@@ -1,3 +0,0 @@
1
- import { MonoTypeOperatorFunction } from "rxjs";
2
- import { TokenProvider } from "@leancodepl/cqrs-client-base";
3
- export default function authGuard<T>(tokenProvider: TokenProvider): MonoTypeOperatorFunction<T>;
@@ -1,3 +0,0 @@
1
- import { OperatorFunction } from "rxjs";
2
- import type { ReducerDescription, ValidationErrorsHandler } from "@leancodepl/validation";
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>;
@@ -1,20 +0,0 @@
1
- import { AjaxConfig } from "rxjs/ajax";
2
- import { CommandResult, TokenProvider } from "@leancodepl/cqrs-client-base";
3
- export declare function mkCqrsClient({ cqrsEndpoint, tokenProvider, ajaxOptions, tokenHeader, }: {
4
- cqrsEndpoint: string;
5
- tokenProvider?: TokenProvider;
6
- ajaxOptions?: Omit<AjaxConfig, "body" | "headers" | "method" | "responseType" | "url">;
7
- tokenHeader?: string;
8
- }): {
9
- createQuery<TQuery, TResult>(type: string): (dto: TQuery) => import("rxjs").Observable<TResult>;
10
- createOperation<TOperation, TResult>(type: string): (dto: TOperation) => import("rxjs").Observable<TResult>;
11
- createCommand<TCommand, TErrorCodes extends {
12
- [name: string]: number;
13
- }>(type: string, errorCodesMap: TErrorCodes): {
14
- (dto: TCommand): import("rxjs").Observable<CommandResult<TErrorCodes>>;
15
- handle(dto: TCommand): import("rxjs").Observable<import("@leancodepl/validation").ValidationErrorsHandler<TErrorCodes & {
16
- readonly success: -1;
17
- readonly failure: -2;
18
- }, never>>;
19
- };
20
- };
@@ -1,2 +0,0 @@
1
- import { OperatorFunction } from "rxjs";
2
- export declare function reduceBoolean(): OperatorFunction<boolean, boolean>;
@@ -1,4 +0,0 @@
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 {};