tonightpass 0.0.81 → 0.0.83

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.
@@ -1,8 +1,8 @@
1
1
  import { pathcat } from "pathcat";
2
- import { Options, Response } from "redaxios";
2
+ import { Options, Response as RedaxiosResponse } from "redaxios";
3
3
 
4
4
  import { ParamValue, Query } from "..";
5
- import { APIResponse, Endpoints, ErroredAPIResponse } from "./endpoints";
5
+ import { Endpoints } from "./endpoints";
6
6
  import { APIRequestOptions, request } from "./request";
7
7
  import { DEFAULT_API_URL } from "../constants";
8
8
 
@@ -11,11 +11,54 @@ export type PathsFor<M extends Options["method"]> = Extract<
11
11
  { method: M }
12
12
  >["path"];
13
13
 
14
+ export type SuccessfulAPIResponse<T> = {
15
+ success: true;
16
+ data: T;
17
+ };
18
+
19
+ export type ErroredAPIResponse = {
20
+ success: false;
21
+ message: string;
22
+ errors?: {
23
+ [key: string]: string;
24
+ };
25
+ };
26
+
27
+ export type APIResponse<T> = SuccessfulAPIResponse<T> | ErroredAPIResponse;
28
+
29
+ export type PromisedAPIResponse<T> = Promise<APIResponse<T>>;
30
+
31
+ export type Response<
32
+ M extends Options["method"],
33
+ P extends PathsFor<M>,
34
+ > = APIResponse<Extract<Endpoints, { method: M; path: P }>["res"]>;
35
+
36
+ export type PromisedResponse<
37
+ M extends Options["method"],
38
+ P extends PathsFor<M>,
39
+ > = PromisedAPIResponse<Extract<Endpoints, { method: M; path: P }>["res"]>;
40
+
41
+ export type Body<M extends Options["method"], P extends PathsFor<M>> = Extract<
42
+ Endpoints,
43
+ { method: M; path: P }
44
+ >["body"];
45
+
46
+ export type StringifiedQueryValue = string | string[];
47
+
48
+ export type StringifiedQuery<T> = {
49
+ [K in keyof T]: StringifiedQueryValue;
50
+ };
51
+
52
+ export type QueryParams<
53
+ M extends Options["method"],
54
+ P extends PathsFor<M>,
55
+ > = StringifiedQuery<Extract<Endpoints, { method: M; path: P }>["body"]>;
56
+
14
57
  export class TonightPassAPIError<T> extends Error {
15
58
  public readonly status: number;
16
59
 
17
60
  constructor(
18
- public readonly response: Response<APIResponse<T>>,
61
+ public readonly response: RedaxiosResponse<APIResponse<T>>,
19
62
  public readonly data: ErroredAPIResponse,
20
63
  ) {
21
64
  super(data.message);
@@ -49,53 +92,73 @@ export class Client {
49
92
  query?: Query<Path>,
50
93
  options?: APIRequestOptions,
51
94
  ) {
52
- return this.requester<
53
- Extract<Endpoints, { path: Path; method: "GET" }>["res"]
54
- >("GET", path, undefined, query, options);
95
+ return this.requester<Response<"GET", Path>>(
96
+ "GET",
97
+ path,
98
+ undefined,
99
+ query,
100
+ options,
101
+ );
55
102
  }
56
103
 
57
- async post<Path extends Extract<Endpoints, { method: "POST" }>["path"]>(
104
+ async post<Path extends PathsFor<"POST">>(
58
105
  path: Path,
59
- body: Extract<Endpoints, { path: Path; method: "POST" }>["body"],
106
+ body: Body<"POST", Path>,
60
107
  query?: Query<Path>,
61
108
  options?: APIRequestOptions,
62
109
  ) {
63
- return this.requester<
64
- Extract<Endpoints, { path: Path; method: "POST" }>["res"]
65
- >("POST", path, body, query, options);
110
+ return this.requester<Response<"POST", Path>>(
111
+ "POST",
112
+ path,
113
+ body,
114
+ query,
115
+ options,
116
+ );
66
117
  }
67
118
 
68
- async put<Path extends Extract<Endpoints, { method: "PUT" }>["path"]>(
119
+ async put<Path extends PathsFor<"PUT">>(
69
120
  path: Path,
70
- body: Extract<Endpoints, { path: Path; method: "PUT" }>["body"],
121
+ body: Body<"PUT", Path>,
71
122
  query?: Query<Path>,
72
123
  options?: APIRequestOptions,
73
124
  ) {
74
- return this.requester<
75
- Extract<Endpoints, { path: Path; method: "PUT" }>["res"]
76
- >("PUT", path, body, query, options);
125
+ return this.requester<Response<"PUT", Path>>(
126
+ "PUT",
127
+ path,
128
+ body,
129
+ query,
130
+ options,
131
+ );
77
132
  }
78
133
 
79
- async patch<Path extends Extract<Endpoints, { method: "PATCH" }>["path"]>(
134
+ async patch<Path extends PathsFor<"PATCH">>(
80
135
  path: Path,
81
- body: Extract<Endpoints, { path: Path; method: "PATCH" }>["body"],
136
+ body: Body<"PATCH", Path>,
82
137
  query?: Query<Path>,
83
138
  options?: APIRequestOptions,
84
139
  ) {
85
- return this.requester<
86
- Extract<Endpoints, { path: Path; method: "PATCH" }>["res"]
87
- >("PATCH", path, body, query, options);
140
+ return this.requester<Response<"PATCH", Path>>(
141
+ "PATCH",
142
+ path,
143
+ body,
144
+ query,
145
+ options,
146
+ );
88
147
  }
89
148
 
90
- async delete<Path extends Extract<Endpoints, { method: "DELETE" }>["path"]>(
149
+ async delete<Path extends PathsFor<"DELETE">>(
91
150
  path: Path,
92
- body: Extract<Endpoints, { path: Path; method: "DELETE" }>["body"],
151
+ body: Body<"DELETE", Path>,
93
152
  query?: Query<Path>,
94
153
  options?: APIRequestOptions,
95
154
  ) {
96
- return this.requester<
97
- Extract<Endpoints, { path: Path; method: "DELETE" }>["res"]
98
- >("DELETE", path, body, query, options);
155
+ return this.requester<Response<"DELETE", Path>>(
156
+ "DELETE",
157
+ path,
158
+ body,
159
+ query,
160
+ options,
161
+ );
99
162
  }
100
163
 
101
164
  private async requester<T>(
@@ -113,7 +176,7 @@ export class Client {
113
176
  }
114
177
  }
115
178
 
116
- const response: Response<APIResponse<T>> = await request<T>(url, {
179
+ const response: RedaxiosResponse<APIResponse<T>> = await request<T>(url, {
117
180
  method,
118
181
  data: body,
119
182
  ...options,
@@ -12,23 +12,6 @@ import {
12
12
  WebhookEndpoints,
13
13
  } from "./types";
14
14
 
15
- export type SuccessfulAPIResponse<T> = {
16
- success: true;
17
- data: T;
18
- };
19
-
20
- export type ErroredAPIResponse = {
21
- success: false;
22
- message: string;
23
- errors?: {
24
- [key: string]: string;
25
- };
26
- };
27
-
28
- export type APIResponse<T> = SuccessfulAPIResponse<T> | ErroredAPIResponse;
29
-
30
- export type PromisedAPIResponse<T> = Promise<APIResponse<T>>;
31
-
32
15
  export type Endpoint<
33
16
  M extends Options["method"],
34
17
  Path extends string,
@@ -1,7 +1,7 @@
1
1
  import axios, { Options, Response } from "redaxios";
2
2
 
3
3
  import { isBrowser } from "../../utils";
4
- import { APIResponse, ErroredAPIResponse } from "../endpoints";
4
+ import { APIResponse, ErroredAPIResponse } from "../client";
5
5
 
6
6
  const instance = axios.create({
7
7
  headers: {
@@ -36,7 +36,7 @@ export type OrganizationMembersEndpoints =
36
36
  >
37
37
  | Endpoint<
38
38
  "GET",
39
- "/organizations/@me/members",
39
+ "/organizations/members/@me",
40
40
  ArrayResult<OrganizationMember>,
41
41
  ArrayOptions<OrganizationMember>
42
42
  >