From f1e2566cafe5ac90bb7d50bd507f2eec22f26415 Mon Sep 17 00:00:00 2001 From: KChi <59391215+KagChi@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:07:17 +0700 Subject: [PATCH] feat: actual inital commit --- .npmignore | 11 +++++ README.md | 19 ++++++-- package.json | 16 ++++--- src/Structures/REST.ts | 98 ++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 4 +- 5 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 .npmignore create mode 100644 src/Structures/REST.ts diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1b1d767 --- /dev/null +++ b/.npmignore @@ -0,0 +1,11 @@ +.github +src +node_modules +build.config.ts +package-lock.json +tsconfig.json +tsconfig.eslint.json +tsconfig.tsbuildinfo +.gitignore +.gitattributes +.eslintrc \ No newline at end of file diff --git a/README.md b/README.md index 9414ddb..c91348d 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,23 @@ ![Kirishima Banner](https://cdn.discordapp.com/attachments/891939988088975372/931079377771450388/kirishima-ship-banner.png) -# @kirishima/template +# @kirishima/rest + # Features -- lorem -- ipsum \ No newline at end of file +- Written in TypeScript +- Support ESM & CommonJS + +# Example +```ts +import { REST } from "@kirishima/rest"; + +(async () => { + const lavalinkRest = new REST("http://lava.link:80") + .setAuthorization("youshallnotpass") + + const tracks = await lavalinkSocket.loadTracks("never gonna give you up"); + console.log(tracks); +})() \ No newline at end of file diff --git a/package.json b/package.json index 3176415..64b9d86 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,17 @@ { - "name": "kirishima-template", + "name": "@kirishima/rest", "author": { "name": "KagChi" }, "version": "0.1.0", "license": "GPL-3.0", "repository": { - "url": "https://github.com/kirishima-ship/kirishima-template" + "url": "https://github.com/kirishima-ship/rest" }, "bugs": { - "url": "https://github.com/kirishima-ship/kirishima-template/issues" + "url": "https://github.com/kirishima-ship/rest/issues" }, - "readme": "https://github.com/kirishima-ship/kirishima-template/blob/main/README.md", + "readme": "https://github.com/kirishima-ship/rest/blob/main/README.md", "engines": { "node": ">=14.0.0", "npm": ">=7.0.0" @@ -23,6 +23,7 @@ "format": "prettier --write {src,tests}/**/*.ts" }, "devDependencies": { + "@sapphire/async-queue": "^1.2.0", "@sapphire/eslint-config": "4.2.0", "@sapphire/prettier-config": "1.3.0", "@sapphire/ts-config": "3.3.0", @@ -32,5 +33,10 @@ "typescript": "4.5.5", "unbuild": "0.6.9" }, - "prettier": "@sapphire/prettier-config" + "prettier": "@sapphire/prettier-config", + "dependencies": { + "@kirishima/fetch": "^0.2.1", + "lavalink-api-types": "^0.1.3", + "undici": "^4.15.1" + } } diff --git a/src/Structures/REST.ts b/src/Structures/REST.ts new file mode 100644 index 0000000..6c3ae4e --- /dev/null +++ b/src/Structures/REST.ts @@ -0,0 +1,98 @@ +import { + LavalinkSource, + LavalinkSourceEnum, + LoadTrackResponse, + LavalinkSearchIdentifierEnum, + LavalinkTrack, + RoutePlannerStatusResponse +} from 'lavalink-api-types'; +import { fetch, FetchResultTypes } from '@kirishima/fetch'; +import { AsyncQueue } from '@sapphire/async-queue'; +import type { RequestInit } from 'undici'; + +export class REST { + public headers: { [key: string]: string } = {}; + public queue = new AsyncQueue(); + + public routeplanner = { + freeAddress: async (address: string): Promise => { + await this.post('routeplanner/free/address', { body: JSON.stringify({ address }) }); + }, + freeAllAddress: async (): Promise => { + await this.post('routeplanner/free/all'); + }, + status: async (): Promise => { + return this.get('routeplanner/status'); + } + }; + + public constructor(public url: string, headers: { [key: string]: string } = {}) { + this.headers = headers; + } + + public isUrl(url: string) { + try { + new URL(url); + return true; + } catch (_e) { + return false; + } + } + + public resolveIdentifier(source: LavalinkSource) { + return source === LavalinkSourceEnum.Youtube + ? LavalinkSearchIdentifierEnum.YT_SEARCH + : source === LavalinkSourceEnum.Soundcloud + ? LavalinkSearchIdentifierEnum.SC_SEARCH + : source === LavalinkSearchIdentifierEnum.YTM_SEARCH + ? LavalinkSearchIdentifierEnum.YTM_SEARCH + : source; + } + + public setAuthorization(auth: string) { + this.headers['Authorization'] = auth; + return this; + } + + public loadTracks(options: { source?: LavalinkSource; query: string } | string) { + if (typeof options === 'string') { + return this.get(`loadtracks?identifier=${this.resolveIdentifier(LavalinkSourceEnum.Youtube)}:${options}`); + } + const source = options.source ?? LavalinkSourceEnum.Youtube; + const { query } = options; + return this.get( + `loadtracks?identifier=${this.isUrl(options.query) ? query : `${this.resolveIdentifier(source)}:${query}`}` + ); + } + + public decodeTracks(trackOrTracks: LavalinkTrack['track'][] | LavalinkTrack['track']) { + if (Array.isArray(trackOrTracks)) { + return this.post('decodetracks', { + body: JSON.stringify(trackOrTracks), + headers: { ...this.headers, 'Content-Type': 'application/json' } + }); + } + return this.post('decodetracks', { + body: JSON.stringify([trackOrTracks]), + headers: { ...this.headers, 'Content-Type': 'application/json' } + }); + } + + private async get(route: string, init?: RequestInit | undefined): Promise { + await this.queue.wait(); + try { + return fetch(`${this.url}/${route}`, { headers: this.headers, ...init }, FetchResultTypes.JSON); + } finally { + this.queue.shift(); + } + } + + private async post(route: string, init?: RequestInit | undefined): Promise { + await this.queue.wait(); + try { + return fetch(`${this.url}/${route}`, { headers: this.headers, method: 'POST', ...init }, FetchResultTypes.JSON); + } finally { + this.queue.shift(); + } + } +} diff --git a/src/index.ts b/src/index.ts index fa9f0ca..0fd9e70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1 @@ -export const main = () => 'Kirishima ship ready to serve !'; - -export default main; +export { REST } from './Structures/REST';