UNPKG

5.03 kBTypeScriptView Raw
1import { HttpClient } from '@angular/common/http';
2import { Router } from '@angular/router';
3import { Store } from '@ngrx/store';
4import { Observable } from "rxjs";
5import 'rxjs/add/operator/share';
6export declare class ApiHttpService {
7 private httpSvc;
8 private storeSvc;
9 private routerSvc;
10 /** Hold GET requests from an API using the URL as a primary key */
11 protected cache: {
12 [key: string]: Observable<any>;
13 };
14 constructor(httpSvc: HttpClient, storeSvc: Store<IStore.root>, routerSvc: Router);
15 /**
16 * Make a GET request with simple caching
17 * @param url - The URL location of the webapi
18 * @param updateCache - Refresh the version in the cache
19 */
20 get<T>(url: string, updateCache?: boolean): Observable<T>;
21 /**
22 * Make a GET request and load the results into the store
23 * @param url - The URL location of the webapi
24 * @param id - The location to put the results in the store
25 * @param updateCache - Refresh the version in the cache
26 */
27 protected getStore<T>(url: string, apiMap?: IStore.ApiMap, updateCache?: boolean): Observable<T>;
28 /**
29 * Make a POST request and load the results into the store
30 * @param url - The URL location of the endpoint
31 * @param id - The location to put the results in the store
32 * @param data - The data to pass to the server
33 */
34 protected postStore<T>(url: string, apiMap: IStore.ApiMap, data: any): Observable<T>;
35 /**
36 * Make a PUT request
37 * @param url - The URL location of the webapi
38 * @param data - The data to pass to the server
39 */
40 protected putStore<T>(url: string, apiMap: IStore.ApiMap, data: any): Observable<T>;
41 /**
42 * Make a DELETE request
43 * @param url - The URL location of the webapi
44 * @param apiMap - The ApiMap object
45 * @param element - The element or collection of elements being deleted
46 */
47 protected deleteStore<T>(url: string, apiMap: IStore.ApiMap, element: any | any[]): Observable<T>;
48 /**
49 * When an authentication check fails
50 * @param error
51 */
52 private endSession(error);
53}
54export declare namespace IStore {
55 /*************************
56 * App specific interfaces
57 *************************/
58 /** API Store */
59 interface api {
60 users?: Mapped<{
61 user: string;
62 email: string;
63 name: string;
64 phone: string;
65 username: string;
66 website: string;
67 }>;
68 }
69 /** The API Map */
70 interface ApiMapping {
71 users?: ApiMap;
72 }
73 /** UI Store */
74 interface ui {
75 modal?: {
76 modalId: string;
77 options: {};
78 data: any;
79 };
80 }
81 /*************************
82 * Non-customizable interfaces
83 *************************/
84 /** The root store which contains the other stores */
85 interface root {
86 api?: api;
87 ui?: ui;
88 apiStatus?: apiStatus;
89 }
90 /** API status store */
91 interface apiStatus {
92 [key: string]: ApiStatus;
93 }
94 /** Example pattern for data that is mapped before being passed into the store */
95 interface Mapped<T> {
96 /** Unaltered source of API response */
97 src?: T[];
98 /** A dictionary organized by the primary key */
99 dict?: {
100 [key: string]: T;
101 };
102 /** A deduped array arranged into a dictionary by primary key */
103 uniques?: {
104 [key: string]: T;
105 };
106 }
107 interface StateStatuses {
108 users?: ApiStatus;
109 }
110 interface ApiStatus {
111 loading?: boolean;
112 loaded?: boolean;
113 loadError?: any;
114 modifying?: boolean;
115 modified?: boolean;
116 modifyError?: any;
117 }
118 /** Maps the relationship between the store and the API. Automates all the interaction. */
119 interface ApiMap {
120 /** The location of the rest API endpoint */
121 endpoint?: string;
122 /** The location/property of where to put the API response into the store */
123 storeProperty?: string;
124 /** A unique ID of each object in the collection. Also supports an array of strings if multiple unique ID's are needed in the event of a single key not being enough. */
125 uniqueId?: string | string[];
126 /** A callback function to modify the API response before it is inserted into the store */
127 map?: any;
128 /** If a map callback function is specified, this is the key for the location of the original unfiltered list of items. This is necessary to update the mapped list in the store without a GET all */
129 mapSrc?: string;
130 /** Occasionally a unique piece of information needs to be passed to the reducer from the method. This property can have data assigned to pass to the reducer */
131 data?: any;
132 }
133 interface Rest {
134 storeProp: string;
135 path: string;
136 }
137}