consolidation of various example repos/gists

This commit is contained in:
2021-07-15 11:36:18 +02:00
parent 58142d3157
commit 16953bd936
20 changed files with 978 additions and 1 deletions

View File

@@ -0,0 +1,71 @@
import { Injectable } from "@angular/core";
import { Store } from "@ngxs/store";
import { first } from "rxjs/operators";
import { Observable, Subject } from "rxjs";
import { HttpClient, HttpResponse } from "@angular/common/http";
import * as resolvePath from "object-resolve-path";
@Injectable({
providedIn: "root",
})
export abstract class HttpWithStateSettingsService<TSettings> {
constructor(
public httpWithStateSettingsServiceStore: Store,
public httpWithStateSettingsServiceHttp: HttpClient,
public settingsKey: string
) {}
/**
* Higher order component which will resolve the current backend settings from the state
* @param callback
*/
public withSettings(
callback: (settings) => Observable<any>
): Observable<any> {
return this.withState((state) => {
return resolvePath(state, this.settingsKey);
}, callback);
}
/**
* get http response with settings
* @param getUrl function to construct the url. fe: (settings) => { const { root, principals } = settings; return `${root}/${principals}`;}
*/
public getResponse<TResponseType>(
getUrl: (settings: TSettings) => string
): Observable<HttpResponse<TResponseType>> {
return this.withSettings((settings: TSettings) => {
return this.httpWithStateSettingsServiceHttp.get(getUrl(settings), {
observe: "response",
});
}).pipe(first());
}
/**
* Higher order component which will resolve the current backend settings from the state
* @param callback
*/
private withState(
selector: (state) => any,
callback: (settings) => Observable<any>
): Observable<any> {
const subject = new Subject();
this.httpWithStateSettingsServiceStore
.select(selector)
.pipe(first())
.subscribe((data) => {
callback(data).subscribe(
(result) => {
subject.next(result);
subject.complete();
},
(error) => {
subject.error(error);
}
);
});
return subject.asObservable();
}
}

View File

@@ -0,0 +1,66 @@
import { PENDING, FULFILLED, REJECTED } from "redux-promise-middleware";
export default function handleReduxPromiseMiddleware(
reducer,
actionType,
initialState,
statusKey,
meta = {}
) {
it(`should handle ${PENDING} (redux-promise-middleware)`, () => {
let taintedState = Object.assign({}, initialState);
taintedState[statusKey] = "TAINTED";
const pendingAction = {
type: `${actionType}_${PENDING}`,
payload: PENDING,
meta: meta,
};
expect(reducer(taintedState, pendingAction)[statusKey]).toEqual(PENDING);
});
it(`should handle ${REJECTED} (redux-promise-middleware)`, () => {
let taintedState = Object.assign({}, initialState);
taintedState[statusKey] = "TAINTED";
const rejectAction = {
type: `${actionType}_${REJECTED}`,
payload: REJECTED,
meta: meta,
};
expect(reducer(taintedState, rejectAction)[statusKey]).toEqual(REJECTED);
});
it(`should handle ${FULFILLED} (redux-promise-middleware)`, () => {
let taintedState = Object.assign({}, initialState);
taintedState[statusKey] = "TAINTED";
const fulfillAction = {
type: `${actionType}_${FULFILLED}`,
payload: FULFILLED,
meta: meta,
};
expect(reducer(taintedState, fulfillAction)[statusKey]).toEqual(FULFILLED);
});
}
/**
USAGE:
import {
handleReduxPromiseMiddleware
} from "redux-promise-middleware.spec.js";
handleReduxPromiseMiddleware(
reducer,
"ACTION_KEY",
initialState,
"status", {
data: {
access_token: "",
token_type: ""
}
}
);
**/