instapaper-ts 1.1.1 → 1.1.3

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
@@ -1,3 +1,5 @@
1
+ import OAuth from 'oauth-1.0a';
2
+
1
3
  type User = {
2
4
  type: "user";
3
5
  user_id: number;
@@ -85,11 +87,20 @@ declare class Instapaper {
85
87
  private username?;
86
88
  private password?;
87
89
  private token?;
88
- constructor(consumerKey: string, consumerSecret: string);
89
- setCredentials: (username: string, password: string) => void;
90
+ constructor({ consumerKey, consumerSecret, username, password, token, }: {
91
+ consumerKey: string;
92
+ consumerSecret: string;
93
+ username?: string;
94
+ password?: string;
95
+ token?: OAuth.Token;
96
+ });
90
97
  private makeRequest;
91
- private getAccessToken;
98
+ fetchToken: () => Promise<OAuth.Token>;
92
99
  private request;
100
+ setCredentials: (username: string, password: string) => void;
101
+ withCredentials: (username: string, password: string) => this;
102
+ withToken: (token: OAuth.Token) => this;
103
+ verifyCredentials: () => Promise<[User]>;
93
104
  bookmarks: {
94
105
  list: (params?: ListParams) => Promise<(Bookmark | Folder | User | Error | Meta)[]>;
95
106
  updateReadProgress: (params: UpdateReadProgressParams) => Promise<[Bookmark]>;
package/dist/index.js CHANGED
@@ -9,7 +9,13 @@ var Instapaper = class {
9
9
  username;
10
10
  password;
11
11
  token;
12
- constructor(consumerKey, consumerSecret) {
12
+ constructor({
13
+ consumerKey,
14
+ consumerSecret,
15
+ username,
16
+ password,
17
+ token
18
+ }) {
13
19
  this.oauth = new OAuth({
14
20
  consumer: {
15
21
  key: consumerKey,
@@ -18,12 +24,11 @@ var Instapaper = class {
18
24
  signature_method: "HMAC-SHA1",
19
25
  hash_function: (base_string, key) => crypto.createHmac("sha1", key).update(base_string).digest("base64")
20
26
  });
21
- }
22
- setCredentials = (username, password) => {
23
27
  this.username = username;
24
28
  this.password = password;
25
- };
26
- makeRequest = async (url, method = "POST", params = {}, token) => {
29
+ this.token = token;
30
+ }
31
+ makeRequest = async (url, params = {}, token) => {
27
32
  const form = new URLSearchParams();
28
33
  for (const [key, val] of Object.entries(params)) {
29
34
  form.append(key, String(val));
@@ -32,14 +37,14 @@ var Instapaper = class {
32
37
  this.oauth.authorize(
33
38
  {
34
39
  url,
35
- method,
40
+ method: "POST",
36
41
  data: params
37
42
  },
38
43
  token
39
44
  )
40
45
  );
41
46
  const response = await fetch(url, {
42
- method,
47
+ method: "POST",
43
48
  headers: new Headers({ ...headers }),
44
49
  body: form
45
50
  });
@@ -54,7 +59,7 @@ var Instapaper = class {
54
59
  return response.text();
55
60
  }
56
61
  };
57
- getAccessToken = async () => {
62
+ fetchToken = async () => {
58
63
  assert(
59
64
  this.username && this.password,
60
65
  "Please set username and password with setCredentials()."
@@ -64,11 +69,7 @@ var Instapaper = class {
64
69
  x_auth_password: this.password,
65
70
  x_auth_mode: "client_auth"
66
71
  };
67
- const responseText = await this.makeRequest(
68
- this.authUrl,
69
- "POST",
70
- params
71
- );
72
+ const responseText = await this.makeRequest(this.authUrl, params);
72
73
  const data = new URLSearchParams(responseText);
73
74
  const key = data.get("oauth_token");
74
75
  const secret = data.get("oauth_token_secret");
@@ -76,16 +77,28 @@ var Instapaper = class {
76
77
  key && secret,
77
78
  "There was an error fetching the token. One or both of key and secret is null."
78
79
  );
79
- this.token = { key, secret };
80
- return this.token;
80
+ return { key, secret };
81
81
  };
82
82
  request = async (endpoint, params = {}) => {
83
83
  if (!this.token) {
84
- this.token = await this.getAccessToken();
84
+ this.token = await this.fetchToken();
85
85
  }
86
86
  const url = this.baseUrl + endpoint;
87
- return this.makeRequest(url, "POST", params, this.token);
87
+ return this.makeRequest(url, params, this.token);
88
+ };
89
+ setCredentials = (username, password) => {
90
+ this.username = username;
91
+ this.password = password;
92
+ };
93
+ withCredentials = (username, password) => {
94
+ this.setCredentials(username, password);
95
+ return this;
96
+ };
97
+ withToken = (token) => {
98
+ this.token = token;
99
+ return this;
88
100
  };
101
+ verifyCredentials = () => this.request("/1/account/verify_credentials");
89
102
  bookmarks = {
90
103
  list: (params = {}) => this.request(
91
104
  "/1/bookmarks/list",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "instapaper-ts",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "type": "module",
5
5
  "description": "A type-safe API client for Instapaper.",
6
6
  "main": "dist/index.js",