rmapi-js 14.2.0 → 14.3.0

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/dist/index.d.ts CHANGED
@@ -86,6 +86,18 @@ export declare class ResponseError extends Error {
86
86
  readonly statusText: string;
87
87
  constructor(status: number, statusText: string, message: string);
88
88
  }
89
+ /** an error that results from the service rejecting a registration */
90
+ export declare class RegisterError extends ResponseError {
91
+ /** the response body */
92
+ readonly body: string;
93
+ constructor(status: number, statusText: string, body: string);
94
+ }
95
+ /** an error that results from the service refusing to issue a session token */
96
+ export declare class AuthError extends ResponseError {
97
+ /** the response body */
98
+ readonly body: string;
99
+ constructor(status: number, statusText: string, body: string);
100
+ }
89
101
  /** options for registering with the api */
90
102
  export interface RegisterOptions {
91
103
  /**
@@ -110,6 +122,7 @@ export interface RegisterOptions {
110
122
  * token to use the api.
111
123
  *
112
124
  * @param code - the eight letter code a user got from `https://my.remarkable.com/device/browser/connect`.
125
+ * @throws RegisterError if the service rejects the registration
113
126
  * @returns the device token necessary for creating an api instace. These never expire so persist as long as necessary.
114
127
  */
115
128
  export declare function register(code: string, { deviceDesc, uuid, authHost, }?: RegisterOptions): Promise<string>;
@@ -912,6 +925,7 @@ export interface RemarkableOptions extends AuthOptions, RemarkableSessionOptions
912
925
  *
913
926
  * @param deviceToken - the device token proving this api instance is
914
927
  * registered. Create one with {@link register}.
928
+ * @throws AuthError if the service refuses to issue a session token
915
929
  * @returns the session token returned by the reMarkable service
916
930
  */
917
931
  export declare function auth(deviceToken: string, { authHost }?: AuthOptions): Promise<string>;
package/dist/index.js CHANGED
@@ -205,6 +205,24 @@ export class ResponseError extends Error {
205
205
  this.statusText = statusText;
206
206
  }
207
207
  }
208
+ /** an error that results from the service rejecting a registration */
209
+ export class RegisterError extends ResponseError {
210
+ /** the response body */
211
+ body;
212
+ constructor(status, statusText, body) {
213
+ super(status, statusText, "couldn't register api");
214
+ this.body = body;
215
+ }
216
+ }
217
+ /** an error that results from the service refusing to issue a session token */
218
+ export class AuthError extends ResponseError {
219
+ /** the response body */
220
+ body;
221
+ constructor(status, statusText, body) {
222
+ super(status, statusText, "couldn't fetch auth token");
223
+ this.body = body;
224
+ }
225
+ }
208
226
  /**
209
227
  * register a device and get the token needed to access the api
210
228
  *
@@ -213,6 +231,7 @@ export class ResponseError extends Error {
213
231
  * token to use the api.
214
232
  *
215
233
  * @param code - the eight letter code a user got from `https://my.remarkable.com/device/browser/connect`.
234
+ * @throws RegisterError if the service rejects the registration
216
235
  * @returns the device token necessary for creating an api instace. These never expire so persist as long as necessary.
217
236
  */
218
237
  export async function register(code, { deviceDesc = "browser-chrome", uuid = uuid4(), authHost = AUTH_HOST, } = {}) {
@@ -223,6 +242,7 @@ export async function register(code, { deviceDesc = "browser-chrome", uuid = uui
223
242
  method: "POST",
224
243
  headers: {
225
244
  Authorization: "Bearer",
245
+ "Content-Type": "application/json",
226
246
  },
227
247
  body: JSON.stringify({
228
248
  code,
@@ -230,11 +250,12 @@ export async function register(code, { deviceDesc = "browser-chrome", uuid = uui
230
250
  deviceID: uuid,
231
251
  }),
232
252
  });
253
+ const body = await resp.text();
233
254
  if (!resp.ok) {
234
- throw new ResponseError(resp.status, resp.statusText, "couldn't register api");
255
+ throw new RegisterError(resp.status, resp.statusText, body);
235
256
  }
236
257
  else {
237
- return await resp.text();
258
+ return body;
238
259
  }
239
260
  }
240
261
  const tokenClaims = z
@@ -1985,6 +2006,7 @@ function decodeCache(dumped) {
1985
2006
  *
1986
2007
  * @param deviceToken - the device token proving this api instance is
1987
2008
  * registered. Create one with {@link register}.
2009
+ * @throws AuthError if the service refuses to issue a session token
1988
2010
  * @returns the session token returned by the reMarkable service
1989
2011
  */
1990
2012
  export async function auth(deviceToken, { authHost = AUTH_HOST } = {}) {
@@ -1994,10 +2016,13 @@ export async function auth(deviceToken, { authHost = AUTH_HOST } = {}) {
1994
2016
  Authorization: `Bearer ${deviceToken}`,
1995
2017
  },
1996
2018
  });
2019
+ const body = await resp.text();
1997
2020
  if (!resp.ok) {
1998
- throw new Error(`couldn't fetch auth token: ${resp.statusText}`);
2021
+ throw new AuthError(resp.status, resp.statusText, body);
2022
+ }
2023
+ else {
2024
+ return body;
1999
2025
  }
2000
- return await resp.text();
2001
2026
  }
2002
2027
  /**
2003
2028
  * Create an API instance from an existing session token.