@guildedts/rest
Advanced tools
Comparing version 0.2.0 to 0.3.0
# @guildedts/rest | ||
## 0.3.0 | ||
### Minor Changes | ||
- # Features | ||
- Added support for the `Retry-After` header. | ||
## 0.2.0 | ||
@@ -4,0 +12,0 @@ |
@@ -1,32 +0,32 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.GuildedAPIError = void 0; | ||
/** Represents an error that occurred while interacting with the Guilded API. */ | ||
class GuildedAPIError extends Error { | ||
code; | ||
message; | ||
status; | ||
method; | ||
path; | ||
body; | ||
/** | ||
* @param code The error code. | ||
* @param message The error message. | ||
* @param status The status code. | ||
* @param method The method used. | ||
* @param path The path used. | ||
* @param body The body used. | ||
*/ | ||
constructor(code, message, status, method, path, body) { | ||
super(message); | ||
this.code = code; | ||
this.message = message; | ||
this.status = status; | ||
this.method = method; | ||
this.path = path; | ||
this.body = body; | ||
this.name = `GuildedAPIError (${status}) [${code}]`; | ||
} | ||
code; | ||
message; | ||
status; | ||
method; | ||
path; | ||
body; | ||
/** | ||
* @param code The error code. | ||
* @param message The error message. | ||
* @param status The status code. | ||
* @param method The method used. | ||
* @param path The path used. | ||
* @param body The body used. | ||
*/ | ||
constructor(code, message, status, method, path, body) { | ||
super(message); | ||
this.code = code; | ||
this.message = message; | ||
this.status = status; | ||
this.method = method; | ||
this.path = path; | ||
this.body = body; | ||
this.name = `GuildedAPIError (${status}) [${code}]`; | ||
} | ||
} | ||
exports.GuildedAPIError = GuildedAPIError; | ||
//# sourceMappingURL=GuildedAPIError.js.map | ||
//# sourceMappingURL=GuildedAPIError.js.map |
@@ -1,22 +0,40 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
var __createBinding = | ||
(this && this.__createBinding) || | ||
(Object.create | ||
? function (o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { | ||
enumerable: true, | ||
get: function () { | ||
return m[k]; | ||
}, | ||
}; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
} | ||
: function (o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
}); | ||
var __exportStar = | ||
(this && this.__exportStar) || | ||
function (m, exports) { | ||
for (var p in m) | ||
if (p !== 'default' && !Object.prototype.hasOwnProperty.call(exports, p)) | ||
__createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.default = void 0; | ||
var RestManager_1 = require("./RestManager"); | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return RestManager_1.RestManager; } }); | ||
__exportStar(require("./RestManager"), exports); | ||
__exportStar(require("./GuildedAPIError"), exports); | ||
//# sourceMappingURL=index.js.map | ||
var RestManager_1 = require('./RestManager'); | ||
Object.defineProperty(exports, 'default', { | ||
enumerable: true, | ||
get: function () { | ||
return RestManager_1.RestManager; | ||
}, | ||
}); | ||
__exportStar(require('./RestManager'), exports); | ||
__exportStar(require('./GuildedAPIError'), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,10 +0,12 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod }; | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.RestManager = void 0; | ||
const _1 = require("."); | ||
const node_fetch_1 = __importDefault(require("node-fetch")); | ||
const util_1 = require("./util"); | ||
const _1 = require('.'); | ||
const node_fetch_1 = __importDefault(require('node-fetch')); | ||
const util_1 = require('./util'); | ||
/** | ||
@@ -15,93 +17,104 @@ * The REST manager for the Guilded API. | ||
class RestManager { | ||
options; | ||
/** The authoization token for the REST api. */ | ||
token; | ||
/** The API version for the REST api. */ | ||
version; | ||
/** | ||
* @param options The options for the REST manager. | ||
*/ | ||
constructor(options) { | ||
this.options = options; | ||
this.version = options.version; | ||
} | ||
/** The base URL for the REST api. */ | ||
get baseUrl() { | ||
return `https://www.guilded.gg/api/v${this.version}/`; | ||
} | ||
/** | ||
* Set the authorization token for the REST api. | ||
* @param token The authorization token. | ||
* @returns The REST manager. | ||
*/ | ||
setToken(token) { | ||
this.token = token; | ||
return this; | ||
} | ||
/** | ||
* Make a request to the REST api. | ||
* @param path The path to resource. | ||
* @param method The HTTP method. | ||
* @param options The options for the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async fetch(path, method, options = {}, retries = 0) { | ||
const searchParams = new URLSearchParams(); | ||
if (options.params) | ||
Object.entries(options.params).forEach(([key, value]) => searchParams.append(key, value.toString())); | ||
const response = await (0, node_fetch_1.default)(this.baseUrl + path + searchParams, { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.token}`, | ||
}, | ||
body: options.body ? JSON.stringify(options.body) : undefined, | ||
}); | ||
const data = await response.json().catch(() => ({})); | ||
if (!response.ok) { | ||
if (response.status === 429 && retries <= (this.options?.maxRetries ?? 3)) { | ||
await (0, util_1.sleep)(this.options?.retryInterval ?? 3000); | ||
return this.fetch(path, method, options, retries++); | ||
} | ||
throw new _1.GuildedAPIError(data.code, data.message, response.status, method, path, options.body); | ||
} | ||
return data; | ||
} | ||
/** | ||
* Make a GET request to the REST api. | ||
* @param path The path to the resource. | ||
* @param params The query parameters. | ||
* @returns The response from the REST api. | ||
*/ | ||
async get(path, params) { | ||
return this.fetch(path, 'GET', { params }); | ||
} | ||
/** | ||
* Make a POST request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async post(path, body) { | ||
return this.fetch(path, 'POST', { body }); | ||
} | ||
/** | ||
* Make a PUT request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async put(path, body) { | ||
return this.fetch(path, 'PUT', { body }); | ||
} | ||
/** | ||
* Make a DELETE request to the REST api. | ||
* @param path The path to the resource. | ||
* @returns The response from the REST api. | ||
*/ | ||
async delete(path) { | ||
return this.fetch(path, 'DELETE'); | ||
} | ||
options; | ||
/** The authoization token for the REST api. */ | ||
token; | ||
/** The API version for the REST api. */ | ||
version; | ||
/** | ||
* @param options The options for the REST manager. | ||
*/ | ||
constructor(options) { | ||
this.options = options; | ||
this.version = options.version; | ||
} | ||
/** The base URL for the REST api. */ | ||
get baseUrl() { | ||
return `https://www.guilded.gg/api/v${this.version}/`; | ||
} | ||
/** | ||
* Set the authorization token for the REST api. | ||
* @param token The authorization token. | ||
* @returns The REST manager. | ||
*/ | ||
setToken(token) { | ||
this.token = token; | ||
return this; | ||
} | ||
/** | ||
* Make a request to the REST api. | ||
* @param path The path to resource. | ||
* @param method The HTTP method. | ||
* @param options The options for the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async fetch(path, method, options = {}, retries = 0) { | ||
const searchParams = new URLSearchParams(); | ||
if (options.params) | ||
Object.entries(options.params).forEach(([key, value]) => | ||
searchParams.append(key, value.toString()), | ||
); | ||
const response = await (0, node_fetch_1.default)(this.baseUrl + path + searchParams, { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${this.token}`, | ||
}, | ||
body: options.body ? JSON.stringify(options.body) : undefined, | ||
}); | ||
const data = await response.json().catch(() => ({})); | ||
if (!response.ok) { | ||
if (response.status === 429 && retries <= (this.options?.maxRetries ?? 3)) { | ||
const retryAfter = response.headers.get('Retry-After'); | ||
const retryDelay = retryAfter ? parseInt(retryAfter) : undefined; | ||
await (0, util_1.sleep)(retryDelay ?? this.options?.retryInterval ?? 3000); | ||
return this.fetch(path, method, options, retries++); | ||
} | ||
throw new _1.GuildedAPIError( | ||
data.code, | ||
data.message, | ||
response.status, | ||
method, | ||
path, | ||
options.body, | ||
); | ||
} | ||
return data; | ||
} | ||
/** | ||
* Make a GET request to the REST api. | ||
* @param path The path to the resource. | ||
* @param params The query parameters. | ||
* @returns The response from the REST api. | ||
*/ | ||
async get(path, params) { | ||
return this.fetch(path, 'GET', { params }); | ||
} | ||
/** | ||
* Make a POST request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async post(path, body) { | ||
return this.fetch(path, 'POST', { body }); | ||
} | ||
/** | ||
* Make a PUT request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
async put(path, body) { | ||
return this.fetch(path, 'PUT', { body }); | ||
} | ||
/** | ||
* Make a DELETE request to the REST api. | ||
* @param path The path to the resource. | ||
* @returns The response from the REST api. | ||
*/ | ||
async delete(path) { | ||
return this.fetch(path, 'DELETE'); | ||
} | ||
} | ||
exports.RestManager = RestManager; | ||
//# sourceMappingURL=RestManager.js.map | ||
//# sourceMappingURL=RestManager.js.map |
@@ -1,3 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.sleep = void 0; | ||
@@ -10,2 +10,2 @@ /** | ||
exports.sleep = sleep; | ||
//# sourceMappingURL=util.js.map | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@guildedts/rest", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "The REST API manager for Guilded.TS.", | ||
@@ -24,11 +24,10 @@ "main": "lib/index.js", | ||
"contributors": [ | ||
"Gamertike <contact@gamertike.com> (https://gamertike.com)", | ||
"Bruno Lepis (https://brunolepis.xyz)" | ||
"Gamertike (https://gamertike.com)" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GuildedTS/Guilded.TS.git" | ||
"url": "https://github.com/guildedts/guilded.ts.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/GuildedTS/Guilded.TS/issues" | ||
"url": "https://github.com/guildedts/guilded.ts/issues" | ||
}, | ||
@@ -38,3 +37,3 @@ "homepage": "https://guildedts.js.org", | ||
"dependencies": { | ||
"node-fetch": "cjs" | ||
"node-fetch": "^2.6.7" | ||
}, | ||
@@ -41,0 +40,0 @@ "devDependencies": { |
@@ -19,2 +19,3 @@ <div align="center"> | ||
- [Guilded API Guilded server](https://www.guilded.gg/API-Official) | ||
- [Guide](https://guide.guildedts.js.org) | ||
- [Documentation](https://guildedts.js.org) | ||
@@ -32,29 +33,6 @@ | ||
Contributing helps us maintain Guilded.TS. All contributions are greatly appreciated. | ||
[Contribute to Guilded.TS.](https://github.com/guildedts/guilded.ts/tree/main/.github/CONTRIBUTING.md) | ||
We utilize [Yarn](https://yarnpkg.com) and [Turbo](https://turborepo.org) to manage our Monorepo. If you want to contribute, we highly recommend knowing the basics of these two. | ||
## Getting started | ||
To get started, run the following script: | ||
``` | ||
# Install all dependencies | ||
yarn install | ||
# Build all local packages | ||
yarn build | ||
``` | ||
## Committing your changes | ||
After you have made your desired changes, make sure you run the following script before committing your changes: | ||
``` | ||
# Prepare your code | ||
# Runs prettier along with eslint | ||
yarn prepare | ||
``` | ||
--- | ||
**Maintained by [Gamertike](https://www.gamertike.com). | Inspired by [discord.js](https://discord.js.org).** |
/** Represents an error that occurred while interacting with the Guilded API. */ | ||
export declare class GuildedAPIError extends Error { | ||
readonly code: string; | ||
readonly message: string; | ||
readonly status: number; | ||
readonly method: string; | ||
readonly path: string; | ||
readonly body?: any; | ||
/** | ||
* @param code The error code. | ||
* @param message The error message. | ||
* @param status The status code. | ||
* @param method The method used. | ||
* @param path The path used. | ||
* @param body The body used. | ||
*/ | ||
constructor(code: string, message: string, status: number, method: string, path: string, body?: any); | ||
readonly code: string; | ||
readonly message: string; | ||
readonly status: number; | ||
readonly method: string; | ||
readonly path: string; | ||
readonly body?: any; | ||
/** | ||
* @param code The error code. | ||
* @param message The error message. | ||
* @param status The status code. | ||
* @param method The method used. | ||
* @param path The path used. | ||
* @param body The body used. | ||
*/ | ||
constructor( | ||
code: string, | ||
message: string, | ||
status: number, | ||
method: string, | ||
path: string, | ||
body?: any, | ||
); | ||
} | ||
//# sourceMappingURL=GuildedAPIError.d.ts.map | ||
//# sourceMappingURL=GuildedAPIError.d.ts.map |
export { RestManager as default } from './RestManager'; | ||
export * from './RestManager'; | ||
export * from './GuildedAPIError'; | ||
//# sourceMappingURL=index.d.ts.map | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,71 +6,79 @@ /** | ||
export declare class RestManager { | ||
options: RestManagerOptions; | ||
/** The authoization token for the REST api. */ | ||
token?: string; | ||
/** The API version for the REST api. */ | ||
readonly version: number; | ||
/** | ||
* @param options The options for the REST manager. | ||
*/ | ||
constructor(options: RestManagerOptions); | ||
/** The base URL for the REST api. */ | ||
get baseUrl(): `https://www.guilded.gg/api/v${number}/`; | ||
/** | ||
* Set the authorization token for the REST api. | ||
* @param token The authorization token. | ||
* @returns The REST manager. | ||
*/ | ||
setToken(token?: string): this; | ||
/** | ||
* Make a request to the REST api. | ||
* @param path The path to resource. | ||
* @param method The HTTP method. | ||
* @param options The options for the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
fetch<R = any, B = any, P extends Record<string, any> = Record<string, any>>(path: string, method: string, options?: FetchOptions<B, P>, retries?: number): Promise<R>; | ||
/** | ||
* Make a GET request to the REST api. | ||
* @param path The path to the resource. | ||
* @param params The query parameters. | ||
* @returns The response from the REST api. | ||
*/ | ||
get<R = any, P extends Record<string, any> = Record<string, any>>(path: string, params?: P): Promise<R>; | ||
/** | ||
* Make a POST request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
post<R = any, B = any>(path: string, body?: B): Promise<R>; | ||
/** | ||
* Make a PUT request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
put<R = any, B = any>(path: string, body?: B): Promise<R>; | ||
/** | ||
* Make a DELETE request to the REST api. | ||
* @param path The path to the resource. | ||
* @returns The response from the REST api. | ||
*/ | ||
delete<R>(path: string): Promise<R>; | ||
options: RestManagerOptions; | ||
/** The authoization token for the REST api. */ | ||
token?: string; | ||
/** The API version for the REST api. */ | ||
readonly version: number; | ||
/** | ||
* @param options The options for the REST manager. | ||
*/ | ||
constructor(options: RestManagerOptions); | ||
/** The base URL for the REST api. */ | ||
get baseUrl(): `https://www.guilded.gg/api/v${number}/`; | ||
/** | ||
* Set the authorization token for the REST api. | ||
* @param token The authorization token. | ||
* @returns The REST manager. | ||
*/ | ||
setToken(token?: string): this; | ||
/** | ||
* Make a request to the REST api. | ||
* @param path The path to resource. | ||
* @param method The HTTP method. | ||
* @param options The options for the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
fetch<R = any, B = any, P extends Record<string, any> = Record<string, any>>( | ||
path: string, | ||
method: string, | ||
options?: FetchOptions<B, P>, | ||
retries?: number, | ||
): Promise<R>; | ||
/** | ||
* Make a GET request to the REST api. | ||
* @param path The path to the resource. | ||
* @param params The query parameters. | ||
* @returns The response from the REST api. | ||
*/ | ||
get<R = any, P extends Record<string, any> = Record<string, any>>( | ||
path: string, | ||
params?: P, | ||
): Promise<R>; | ||
/** | ||
* Make a POST request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
post<R = any, B = any>(path: string, body?: B): Promise<R>; | ||
/** | ||
* Make a PUT request to the REST api. | ||
* @param path The path to the resource. | ||
* @param body The body of the request. | ||
* @returns The response from the REST api. | ||
*/ | ||
put<R = any, B = any>(path: string, body?: B): Promise<R>; | ||
/** | ||
* Make a DELETE request to the REST api. | ||
* @param path The path to the resource. | ||
* @returns The response from the REST api. | ||
*/ | ||
delete<R>(path: string): Promise<R>; | ||
} | ||
/** The options for the REST manager. */ | ||
export interface RestManagerOptions { | ||
/** The API version for the REST api. */ | ||
version: number; | ||
/** The interval to wait between retries. */ | ||
retryInterval?: number; | ||
/** The maximum number of retries. */ | ||
maxRetries?: number; | ||
/** The API version for the REST api. */ | ||
version: number; | ||
/** The interval to wait between retries. */ | ||
retryInterval?: number; | ||
/** The maximum number of retries. */ | ||
maxRetries?: number; | ||
} | ||
/** The options for making a request to the REST api. */ | ||
export interface FetchOptions<B = any, P extends Record<string, any> = Record<string, any>> { | ||
/** The URL query parameters. */ | ||
params?: P; | ||
/** The body of the request. */ | ||
body?: B; | ||
/** The URL query parameters. */ | ||
params?: P; | ||
/** The body of the request. */ | ||
body?: B; | ||
} | ||
//# sourceMappingURL=RestManager.d.ts.map | ||
//# sourceMappingURL=RestManager.d.ts.map |
@@ -6,2 +6,2 @@ /** | ||
export declare const sleep: (ms: number) => Promise<unknown>; | ||
//# sourceMappingURL=util.d.ts.map | ||
//# sourceMappingURL=util.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
320
19257
37
+ Addednode-fetch@2.7.0(transitive)
- Removednode-fetch@2.6.7(transitive)
Updatednode-fetch@^2.6.7