@wise-old-man/utils 2.2.3 → 3.0.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
@@ -292,7 +292,6 @@ type Prisma_Base_Snapshot = {
292
292
  guardians_of_the_riftRank: number;
293
293
  };
294
294
 
295
- import { AxiosInstance } from 'axios';
296
295
 
297
296
  /**
298
297
  * Prisma currently seems to ignore the @map() in enum declarations.
@@ -1866,8 +1865,11 @@ interface DeltaLeaderboardFilter extends BasePlayerFilter {
1866
1865
  }
1867
1866
 
1868
1867
  declare class BaseAPIClient {
1869
- private axiosInstance;
1870
- constructor(axiosInstance: AxiosInstance);
1868
+ private headers;
1869
+ private baseUrl;
1870
+ constructor(headers: object, baseUrl: string);
1871
+ private buildParams;
1872
+ private request;
1871
1873
  postRequest<T>(path: string, body?: unknown): Promise<T>;
1872
1874
  putRequest<T>(path: string, body?: unknown): Promise<T>;
1873
1875
  deleteRequest<T>(path: string, body?: unknown): Promise<T>;
package/dist/index.js CHANGED
@@ -2,13 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var axios = require('axios');
6
5
  var dayjs = require('dayjs');
7
6
  var customParseFormatPlugin = require('dayjs/plugin/customParseFormat');
8
7
 
9
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
9
 
11
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
12
10
  var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
13
11
  var customParseFormatPlugin__default = /*#__PURE__*/_interopDefaultLegacy(customParseFormatPlugin);
14
12
 
@@ -32,6 +30,18 @@ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
32
30
  PERFORMANCE OF THIS SOFTWARE.
33
31
  ***************************************************************************** */
34
32
 
33
+ function __rest(s, e) {
34
+ var t = {};
35
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
36
+ t[p] = s[p];
37
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
38
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
39
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
40
+ t[p[i]] = s[p[i]];
41
+ }
42
+ return t;
43
+ }
44
+
35
45
  function __awaiter(thisArg, _arguments, P, generator) {
36
46
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
47
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -62,25 +72,23 @@ function isValidISODate(input) {
62
72
  function transformDates(input) {
63
73
  return traverseTransform(input, val => (isValidISODate(val) ? new Date(val) : val));
64
74
  }
65
- function handleError(requestURL, e) {
66
- var _a;
67
- if (!((_a = e.response) === null || _a === void 0 ? void 0 : _a.data))
75
+ function handleError(status, path, data) {
76
+ if (!data)
68
77
  return;
69
- const data = e.response.data;
70
- if (e.response.status === 400) {
71
- throw new BadRequestError(requestURL, data.message, data.data);
78
+ if (status === 400) {
79
+ throw new BadRequestError(path, data.message, data.data);
72
80
  }
73
- if (e.response.status === 403) {
74
- throw new ForbiddenError(requestURL, data.message);
81
+ if (status === 403) {
82
+ throw new ForbiddenError(path, data.message);
75
83
  }
76
- if (e.response.status === 404) {
77
- throw new NotFoundError(requestURL, data.message);
84
+ if (status === 404) {
85
+ throw new NotFoundError(path, data.message);
78
86
  }
79
- if (e.response.status === 429) {
80
- throw new RateLimitError(requestURL, data.message);
87
+ if (status === 429) {
88
+ throw new RateLimitError(path, data.message);
81
89
  }
82
- if (e.response.status === 500) {
83
- throw new InternalServerError(requestURL, data.message);
90
+ if (status === 500) {
91
+ throw new InternalServerError(path, data.message);
84
92
  }
85
93
  }
86
94
  class BadRequestError extends Error {
@@ -126,55 +134,55 @@ class InternalServerError extends Error {
126
134
  }
127
135
 
128
136
  class BaseAPIClient {
129
- constructor(axiosInstance) {
130
- this.axiosInstance = axiosInstance;
137
+ constructor(headers, baseUrl) {
138
+ this.baseUrl = baseUrl;
139
+ this.headers = Object.assign({ Accept: 'application/json', 'Content-Type': 'application/json' }, headers);
140
+ }
141
+ buildParams(_a) {
142
+ var params = __rest(_a, []);
143
+ const builder = new URLSearchParams();
144
+ Object.keys(params)
145
+ .filter(k => params[k] !== undefined)
146
+ .forEach(k => builder.set(k, params[k]));
147
+ const query = builder.toString();
148
+ return query ? `?${query}` : '';
149
+ }
150
+ request({ method, path, body, params }) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ const req = { method, body: undefined, headers: this.headers };
153
+ let query = '';
154
+ if (body) {
155
+ req.body = JSON.stringify(body);
156
+ }
157
+ if (params) {
158
+ query = this.buildParams(params);
159
+ }
160
+ const res = yield fetch(this.baseUrl + path + query, req);
161
+ const data = yield res.json();
162
+ if (res.ok) {
163
+ return transformDates(data);
164
+ }
165
+ handleError(res.status, path, data);
166
+ });
131
167
  }
132
168
  postRequest(path, body) {
133
169
  return __awaiter(this, void 0, void 0, function* () {
134
- return this.axiosInstance
135
- .post(path, body || {})
136
- .then(response => transformDates(response.data))
137
- .catch(e => {
138
- if (axios__default["default"].isAxiosError(e))
139
- handleError(path, e);
140
- throw e;
141
- });
170
+ return yield this.request({ method: 'POST', path, body: body || {} });
142
171
  });
143
172
  }
144
173
  putRequest(path, body) {
145
174
  return __awaiter(this, void 0, void 0, function* () {
146
- return this.axiosInstance
147
- .put(path, body || {})
148
- .then(response => transformDates(response.data))
149
- .catch(e => {
150
- if (axios__default["default"].isAxiosError(e))
151
- handleError(path, e);
152
- throw e;
153
- });
175
+ return yield this.request({ method: 'PUT', path, body: body || {} });
154
176
  });
155
177
  }
156
178
  deleteRequest(path, body) {
157
179
  return __awaiter(this, void 0, void 0, function* () {
158
- return this.axiosInstance
159
- .delete(path, { data: body })
160
- .then(response => transformDates(response.data))
161
- .catch(e => {
162
- if (axios__default["default"].isAxiosError(e))
163
- handleError(path, e);
164
- throw e;
165
- });
180
+ return yield this.request({ method: 'DELETE', path, body: body || {} });
166
181
  });
167
182
  }
168
183
  getRequest(path, params) {
169
184
  return __awaiter(this, void 0, void 0, function* () {
170
- return this.axiosInstance
171
- .get(path, { params })
172
- .then(response => transformDates(response.data))
173
- .catch(e => {
174
- if (axios__default["default"].isAxiosError(e))
175
- handleError(path, e);
176
- throw e;
177
- });
185
+ return yield this.request({ method: 'GET', path, params });
178
186
  });
179
187
  }
180
188
  }
@@ -2542,24 +2550,21 @@ class CompetitionsClient extends BaseAPIClient {
2542
2550
 
2543
2551
  class WOMClient extends BaseAPIClient {
2544
2552
  constructor(options) {
2553
+ const baseApiUrl = (options === null || options === void 0 ? void 0 : options.baseAPIUrl) || config.baseAPIUrl;
2545
2554
  const headers = {
2546
2555
  'x-user-agent': (options === null || options === void 0 ? void 0 : options.userAgent) || config.defaultUserAgent
2547
2556
  };
2548
2557
  if (options === null || options === void 0 ? void 0 : options.apiKey) {
2549
2558
  headers['x-api-key'] = options.apiKey;
2550
2559
  }
2551
- const axiosInstance = axios__default["default"].create({
2552
- baseURL: (options === null || options === void 0 ? void 0 : options.baseAPIUrl) || config.baseAPIUrl,
2553
- headers
2554
- });
2555
- super(axiosInstance);
2556
- this.deltas = new DeltasClient(axiosInstance);
2557
- this.groups = new GroupsClient(axiosInstance);
2558
- this.players = new PlayersClient(axiosInstance);
2559
- this.records = new RecordsClient(axiosInstance);
2560
- this.efficiency = new EfficiencyClient(axiosInstance);
2561
- this.nameChanges = new NameChangesClient(axiosInstance);
2562
- this.competitions = new CompetitionsClient(axiosInstance);
2560
+ super(headers, baseApiUrl);
2561
+ this.deltas = new DeltasClient(headers, baseApiUrl);
2562
+ this.groups = new GroupsClient(headers, baseApiUrl);
2563
+ this.players = new PlayersClient(headers, baseApiUrl);
2564
+ this.records = new RecordsClient(headers, baseApiUrl);
2565
+ this.efficiency = new EfficiencyClient(headers, baseApiUrl);
2566
+ this.nameChanges = new NameChangesClient(headers, baseApiUrl);
2567
+ this.competitions = new CompetitionsClient(headers, baseApiUrl);
2563
2568
  }
2564
2569
  }
2565
2570
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wise-old-man/utils",
3
- "version": "2.2.3",
3
+ "version": "3.0.0",
4
4
  "description": "A JavaScript/TypeScript client that interfaces and consumes the Wise Old Man API, an API that tracks and measures players' progress in Old School Runescape.",
5
5
  "keywords": [
6
6
  "wiseoldman",
@@ -41,7 +41,6 @@
41
41
  "typescript": "^4.7.4"
42
42
  },
43
43
  "dependencies": {
44
- "axios": "^0.27.2",
45
44
  "dayjs": "^1.11.5"
46
45
  }
47
46
  }