@xy-planning-network/trees 0.6.2-rc-1 → 0.6.2-rc-2

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@xy-planning-network/trees",
3
- "version": "0.6.2-rc-1",
3
+ "version": "0.6.2-rc-2",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "repository": "github:xy-planning-network/trees",
@@ -1,36 +1,5 @@
1
- import { Pagination } from "../composables/nav";
2
1
  import { AxiosRequestConfig } from "axios";
3
- export interface Paginated<T> extends Pagination {
4
- items: T[];
5
- }
6
- export interface TreesResponse<T = any> {
7
- /**
8
- * response data as T
9
- */
10
- data: T;
11
- /**
12
- * http status code
13
- */
14
- status: number;
15
- }
16
- export interface TreesPromise<T = any> extends Promise<TreesResponse<T>> {
17
- }
18
- export declare type RequestMethod = "GET" | "get" | "PUT" | "put" | "POST" | "post" | "DELETE" | "delete";
19
- export interface RequestOptions {
20
- /**
21
- * disables the full screen loading interface during the request when true
22
- */
23
- skipLoader?: boolean;
24
- /**
25
- * artificially delay's the request by the time specified when set
26
- */
27
- withDelay?: number;
28
- }
29
- declare const BaseAPI: {
30
- makeRequest<T = any>(config: AxiosRequestConfig, opts: RequestOptions): TreesPromise<T>;
31
- get<T_1 = any>(path: string, opts: RequestOptions, params?: Record<string, unknown> | undefined): TreesPromise<T_1>;
32
- delete<T_2 = any>(path: string, opts: RequestOptions): TreesPromise<T_2>;
33
- post<T_3 = any>(path: string, data: Record<string, unknown> | FormData, opts: RequestOptions): TreesPromise<T_3>;
34
- put<T_4 = any>(path: string, data: Record<string, unknown> | FormData, opts: RequestOptions): TreesPromise<T_4>;
35
- };
2
+ import { HttpClient, HttpPromise, RequestOptions } from "./client";
3
+ export declare const httpRequest: <T = any>(config: AxiosRequestConfig, opts: RequestOptions) => HttpPromise<T>;
4
+ declare const BaseAPI: HttpClient;
36
5
  export default BaseAPI;
@@ -0,0 +1,163 @@
1
+ /**
2
+ * RequestMethod
3
+ * The HTTP request methods that our http client supports.
4
+ */
5
+ export declare type RequestMethod = "GET" | "get" | "PATCH" | "patch" | "PUT" | "put" | "POST" | "post" | "DELETE" | "delete";
6
+ /**
7
+ * RequestOptions
8
+ * The set of options available for any http client request.
9
+ */
10
+ export interface RequestOptions {
11
+ /**
12
+ * Disable the full screen loading interface during the request when true.
13
+ */
14
+ skipLoader?: boolean;
15
+ /**
16
+ * Artificially delay a request by the time specified when set.
17
+ */
18
+ withDelay?: number;
19
+ }
20
+ export declare const HTTP_ERROR = "HttpError";
21
+ export declare const HTTP_CANCELLED_ERROR = "HttpCanceledError";
22
+ /**
23
+ * HttpError
24
+ * An http client error when the request is rejected.
25
+ */
26
+ export declare class HttpError<T = unknown> extends Error {
27
+ /**
28
+ * The http response status code.
29
+ */
30
+ status: number;
31
+ /**
32
+ * The http response body.
33
+ */
34
+ response?: T;
35
+ constructor(message?: string, status?: number, response?: T, name?: string);
36
+ }
37
+ /**
38
+ * HttpPromise
39
+ * The successfully resolved interface of an http client request.
40
+ */
41
+ export interface HttpPromise<T = any> extends Promise<T> {
42
+ }
43
+ /**
44
+ * TrailsResponse
45
+ * A convenience interface to represent the shape of a Trails delivered API response.
46
+ *
47
+ * Example Usage:
48
+ * interface User {
49
+ * id: number
50
+ * email: string
51
+ * }
52
+ *
53
+ * BaseAPI.get<TrailsResponse<User>>(`/user/${id}`)
54
+ * .then(result => {
55
+ * const id = result.data.id
56
+ * const email = result.data.email
57
+ * const user = { ...result.data }
58
+ * })
59
+ */
60
+ export interface TrailsResponse<T = any> {
61
+ data: T;
62
+ }
63
+ /**
64
+ * TrailsResponsePaged
65
+ * A convenience interface to represent the shape of a paginated Trails delivered API response.
66
+ *
67
+ * Example Usage:
68
+ * interface User {
69
+ * id: number
70
+ * email: string
71
+ * }
72
+ *
73
+ * BaseAPI.get<TrailsResponsePaged<User>>(`/users`)
74
+ * .then(result => {
75
+ * const currentPage = result.data.page
76
+ * const users = { ...result.data.items }
77
+ *
78
+ * result.data.items.forEach(u => {
79
+ * console.log(u.id)
80
+ * })
81
+ * })
82
+ */
83
+ export interface TrailsResponsePaged<T = any> {
84
+ data: {
85
+ items: T[];
86
+ page: number;
87
+ perPage: number;
88
+ totalItems: number;
89
+ totalPages: number;
90
+ };
91
+ }
92
+ /**
93
+ * QueryParams
94
+ * Http GET request query params.
95
+ */
96
+ export declare type QueryParams = Record<string, any>;
97
+ /**
98
+ * RequestPayload
99
+ * Http POST and PUT payloads.
100
+ */
101
+ export declare type RequestPayload = Record<string, any> | FormData;
102
+ /**
103
+ * HttpClient
104
+ * The http client interface the BaseAPI implements.
105
+ */
106
+ export interface HttpClient {
107
+ /**
108
+ * The method to make an http GET request.
109
+ * @param path string
110
+ * @param opts RequestOptions
111
+ * @param params QueryParams
112
+ * @returns HttpPromise<T>
113
+ */
114
+ get<T>(path: string, opts?: RequestOptions, params?: QueryParams): HttpPromise<T>;
115
+ /**
116
+ * The method to make an http DELETE request.
117
+ * @param path string
118
+ * @param opts RequestOptions
119
+ * @returns HttpPromise<T>
120
+ */
121
+ delete<T>(path: string, opts?: RequestOptions): HttpPromise<T>;
122
+ /**
123
+ * A convenience method for checking if a variable in a failed request has an http status code.
124
+ * This is most useful for checking for specific http status codes in error callbacks.
125
+ * @param err unknown
126
+ * @param codes number | number[]
127
+ * @returns boolean
128
+ */
129
+ hasErrStatus(err: unknown, codes: number | number[]): boolean;
130
+ /**
131
+ * A type guard for checking if a variable is in the shape of a HttpError
132
+ * @param err unknown
133
+ * @returns payload is HttpError
134
+ */
135
+ isHttpError(err: unknown): err is HttpError;
136
+ /**
137
+ * A convenience method for checking if a variable is a cancelled http request error.
138
+ * @param err unknown
139
+ * @returns boolean
140
+ */
141
+ isHttpCancel(err: unknown): boolean;
142
+ /**
143
+ * The method to make an http PATCH request.
144
+ * @param path string
145
+ * @param data RequestPayload
146
+ * @param opts RequestOptions
147
+ */
148
+ patch<T>(path: string, data?: RequestPayload, opts?: RequestOptions): HttpPromise<T>;
149
+ /**
150
+ * The method to make an http POST request.
151
+ * @param path string
152
+ * @param data RequestPayload
153
+ * @param opts RequestOptions
154
+ */
155
+ post<T>(path: string, data?: RequestPayload, opts?: RequestOptions): HttpPromise<T>;
156
+ /**
157
+ * The method to make an http PUT request.
158
+ * @param path string
159
+ * @param data RequestPayload
160
+ * @param opts RequestOptions
161
+ */
162
+ put<T>(path: string, data?: RequestPayload, opts?: RequestOptions): HttpPromise<T>;
163
+ }
@@ -1,7 +1,5 @@
1
- import { AxiosError } from "axios";
2
1
  import { Ref, ShallowRef } from "vue";
3
- import { TreesPromise } from "../api/base";
4
- import type { RequestMethod, RequestOptions } from "../api/base";
2
+ import type { HttpPromise, HttpError, RequestMethod, RequestOptions, RequestPayload } from "../api/client";
5
3
  /**
6
4
  * UseBaseAPIOptions extends Trees/RequestOptions
7
5
  * these options are used only in the instantiation
@@ -20,17 +18,13 @@ export interface UseBaseAPIOptions extends RequestOptions {
20
18
  */
21
19
  export interface UseBaseAPI<T> {
22
20
  /**
23
- * Promise response data
21
+ * Axios response data
24
22
  */
25
23
  result: Ref<T | undefined>;
26
24
  /**
27
- * Promise response http status code
25
+ * Any errors that may have occurred
28
26
  */
29
- status: Ref<number | undefined>;
30
- /**
31
- * Any errors or rejections that may have occurred
32
- */
33
- error: ShallowRef<AxiosError | Error | undefined>;
27
+ error: ShallowRef<Error | HttpError<T> | undefined>;
34
28
  /**
35
29
  * Indicates if the request has finished
36
30
  */
@@ -56,7 +50,7 @@ export interface UseBaseAPI<T> {
56
50
  * Manually call the axios request
57
51
  * can be used multiple times
58
52
  */
59
- execute: (data?: Record<string, unknown> | FormData, opts?: RequestOptions) => TreesPromise<T>;
53
+ execute: (data?: RequestPayload, opts?: RequestOptions) => HttpPromise<T>;
60
54
  }
61
55
  /**
62
56
  * useBaseAPI is a composable wrapper of BaseAPI
@@ -66,17 +60,7 @@ export interface UseBaseAPI<T> {
66
60
  * @param initConfig {AxiosRequestConfig}
67
61
  * @returns {UseBaseAPI<T>}
68
62
  */
69
- export default function useBaseAPI<T = any>(path: string, method?: RequestMethod, initOpts?: UseBaseAPIOptions): {
70
- result: Ref<T | undefined>;
71
- status: Ref<number | undefined>;
72
- error: ShallowRef<AxiosError<unknown, any> | Error | undefined>;
73
- isFinished: Ref<boolean>;
74
- isLoading: Ref<boolean>;
75
- isAborted: Ref<boolean>;
76
- hasFetched: Ref<boolean>;
77
- abort: () => void;
78
- execute: (data?: Record<string, unknown> | FormData, opts?: RequestOptions) => TreesPromise<T>;
79
- };
63
+ export default function useBaseAPI<T = any>(path: string, method?: RequestMethod, initOpts?: UseBaseAPIOptions): UseBaseAPI<T>;
80
64
  /**
81
65
  * useBaseAPIGet is a convenience function for useBaseAPI
82
66
  * @param path {string} the api path or full url for the
@@ -93,6 +77,14 @@ export declare function useBaseAPIGet<T = any>(url: string, opts?: UseBaseAPIOpt
93
77
  * @returns {UseBaseAPI<T>}
94
78
  */
95
79
  export declare function useBaseAPIDelete<T = any>(url: string, opts?: UseBaseAPIOptions): UseBaseAPI<T>;
80
+ /**
81
+ * useBaseAPIPatch is a convenience function for useBaseAPI
82
+ * @param path {string} the api path or full url for the
83
+ * @param initOpts {UseBaseAPIOptions}
84
+ * @param initConfig {AxiosRequestConfig}
85
+ * @returns {UseBaseAPI<T>}
86
+ */
87
+ export declare function useBaseAPIPatch<T = any>(url: string, opts?: UseBaseAPIOptions): UseBaseAPI<T>;
96
88
  /**
97
89
  * useBaseAPIPost is a convenience function for useBaseAPI
98
90
  * @param path {string} the api path or full url for the
package/types/entry.d.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import { Plugin } from "vue";
2
2
  import BaseAPI from "./api/base";
3
- import type { Paginated, RequestOptions, TreesPromise, TreesResponse } from "./api/base";
4
- import type { Pagination } from "./composables/nav";
3
+ import type { HttpPromise, HttpError, RequestOptions, TrailsResponse, TrailsResponsePaged } from "./api/client";
5
4
  declare const install: Exclude<Plugin["install"], undefined>;
6
5
  export default install;
7
6
  export * from "./composables/index";
8
7
  export * from "./lib-components/index";
9
8
  export { BaseAPI };
10
- export type { Paginated, Pagination, RequestOptions, TreesPromise, TreesResponse, };
9
+ export type { HttpPromise, HttpError, RequestOptions, TrailsResponse, TrailsResponsePaged, };