ngx-http-request-state
Version:
Angular library for wrapping HttpClient responses with loading & error information to allow observing load/loading/error state changes as a stream of events.
1 lines • 8.3 kB
Source Map (JSON)
{"version":3,"file":"ngx-http-request-state.mjs","sources":["../../../../libs/ngx-http-request-state/src/lib/builders.ts","../../../../libs/ngx-http-request-state/src/lib/type-guards.ts","../../../../libs/ngx-http-request-state/src/lib/operators.ts","../../../../libs/ngx-http-request-state/src/lib/merge.ts","../../../../libs/ngx-http-request-state/src/ngx-http-request-state.ts"],"sourcesContent":["import {\n ErrorState,\n ErrorStateWithValue,\n LoadedState,\n LoadingState,\n LoadingStateWithValue,\n} from './model';\nimport { HttpErrorResponse } from '@angular/common/http';\n\n/**\n * Returns a new LoadingState instance with an optional last-known value.\n *\n * @param value may be provided to indicate the previously-loaded, or last-known, state\n */\nexport function loadingState<T>(value: T): LoadingStateWithValue<T>;\nexport function loadingState<T = never>(): LoadingState<T>;\nexport function loadingState<T = never>(value?: T): LoadingState<T> {\n return {\n isLoading: true,\n value,\n error: undefined,\n };\n}\n\n/**\n * Returns a new LoadedState instance, with the optional value as the loaded data.\n *\n * @param value\n */\nexport function loadedState<T>(value: T): LoadedState<T> {\n return {\n isLoading: false,\n error: undefined,\n value,\n };\n}\n\n/**\n * Returns a new ErrorState instance with the given error and optional last-known value.\n *\n * @param error\n * @param value may be provided to indicate the previously-loaded, or last-known, state\n */\nexport function errorState<T>(\n error: HttpErrorResponse | Error,\n value: T\n): ErrorStateWithValue<T>;\nexport function errorState<T = never>(\n error: HttpErrorResponse | Error\n): ErrorState<T>;\nexport function errorState<T = never>(\n error: HttpErrorResponse | Error,\n value?: T\n): ErrorState<T> {\n return {\n isLoading: false,\n error,\n value,\n };\n}\n","import { ErrorState, LoadedState, LoadingState } from './model';\n\nexport function isLoadingState<T>(\n state?: LoadingState<T> | ErrorState<unknown> | LoadedState<unknown>\n): state is LoadingState<T> {\n return !!state && state.isLoading;\n}\n\nexport function isLoadedState<T>(\n state?: LoadedState<T> | LoadingState<unknown> | ErrorState<unknown>\n): state is LoadedState<T> {\n return !!state && !state.isLoading && !state.error;\n}\n\nexport function isErrorState<T>(\n state?: ErrorState<T> | LoadedState<unknown> | LoadingState<unknown>\n): state is ErrorState<T> {\n return !!state && !state.isLoading && !!state.error;\n}\n","import { HttpResponse } from '@angular/common/http';\nimport { Observable, of } from 'rxjs';\nimport { catchError, map, startWith } from 'rxjs/operators';\nimport { HttpRequestState } from './model';\nimport { loadingState, loadedState, errorState } from './builders';\n\nexport function httpRequestStates<T>(): (\n source: Observable<HttpResponse<T> | T>\n) => Observable<HttpRequestState<T>>;\n/**\n *\n * @param mapResponse a function to map from HttpResponse<T> to K\n * @deprecated use rxjs's map() operator to transform the response prior to using httpRequestStates() instead.\n */\nexport function httpRequestStates<T, K>(\n mapResponse: (result: HttpResponse<T>) => K\n): (source: Observable<HttpResponse<T>>) => Observable<HttpRequestState<K>>;\n/**\n *\n * @param mapResponse a function to map from T to K\n * @deprecated use rxjs's map() operator to transform the value prior to using httpRequestStates() instead.\n */\nexport function httpRequestStates<T, K>(\n mapResponse: (result: T) => K\n): (source: Observable<T>) => Observable<HttpRequestState<K>>;\nexport function httpRequestStates<T, K>(\n mapResponse?: (result: HttpResponse<T> | T) => K\n): (\n source: Observable<HttpResponse<T> | T>\n) => Observable<HttpRequestState<T | K>> {\n return (source: Observable<HttpResponse<T> | T>) =>\n source.pipe(\n map((result) =>\n loadedState<T | K>(\n mapResponse\n ? mapResponse(result)\n : result instanceof HttpResponse\n ? (result.body as T)\n : result\n )\n ),\n startWith(loadingState<T | K>()),\n catchError((err) => of(errorState<T | K>(err)))\n );\n}\n","import {\n HttpRequestState,\n LoadedState,\n ErrorState,\n LoadingState,\n} from './model';\nimport { isLoadedState, isErrorState } from './type-guards';\n\n/**\n * Given an array of HttpRequestState<T>, merge them together into a new single HttpRequestState<T>,\n * based on a supplied merging strategy of values (mergeValues) or of errors (mergeErrors).\n *\n * One can think of this function as allowing one to act \"inside\" the states, directly on the values or errors.\n *\n * Use this with combineLatest, instead of forkJoin, to get loading updates.\n *\n * @param states Array of states of the same type that should be merged together.\n * @param mergeValues Handles how to merge values together when all states have loaded.\n * @param mergeErrors Handles how to merge errors together in case one or more of the states end up in ErrorState.\n * If not specified, the first error is simply used as the error of the merged state\n * @returns The merged HttpRequestState<T>\n */\nexport function mergeStates<T>(\n states: HttpRequestState<T>[],\n mergeValues: (states: LoadedState<T>['value'][]) => LoadedState<T>['value'],\n mergeErrors?: (states: ErrorState<T>['error'][]) => ErrorState<T>['error']\n): HttpRequestState<T> {\n if (states.every(isLoadedState)) {\n const state: LoadedState<T> = {\n isLoading: false,\n value: mergeValues(states.map((s) => s.value)),\n error: undefined,\n };\n return state;\n }\n\n if (states.some(isErrorState)) {\n if (mergeErrors === undefined) {\n mergeErrors = (errors: ErrorState<T>['error'][]) => errors[0];\n }\n\n const errorStates = states.filter(isErrorState);\n const state: ErrorState<T> = {\n isLoading: false,\n value: undefined,\n error: mergeErrors(errorStates.map((s) => s.error)),\n };\n return state;\n }\n\n // If one of the state is still not loaded and there are no errors\n // the merged state is still considered loading.\n const state: LoadingState<T> = {\n isLoading: true,\n value: undefined,\n error: undefined,\n };\n\n return state;\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;AAgBM,SAAU,YAAY,CAAY,KAAS,EAAA;IAC/C,OAAO;AACL,QAAA,SAAS,EAAE,IAAI;QACf,KAAK;AACL,QAAA,KAAK,EAAE,SAAS;KACjB;AACH;AAEA;;;;AAIG;AACG,SAAU,WAAW,CAAI,KAAQ,EAAA;IACrC,OAAO;AACL,QAAA,SAAS,EAAE,KAAK;AAChB,QAAA,KAAK,EAAE,SAAS;QAChB,KAAK;KACN;AACH;AAeM,SAAU,UAAU,CACxB,KAAgC,EAChC,KAAS,EAAA;IAET,OAAO;AACL,QAAA,SAAS,EAAE,KAAK;QAChB,KAAK;QACL,KAAK;KACN;AACH;;ACzDM,SAAU,cAAc,CAC5B,KAAoE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