Skip to content

Commit

Permalink
feat: actual inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KagChi committed Mar 11, 2022
1 parent 36708d4 commit f1e2566
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.github
src
node_modules
build.config.ts
package-lock.json
tsconfig.json
tsconfig.eslint.json
tsconfig.tsbuildinfo
.gitignore
.gitattributes
.eslintrc
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

![Kirishima Banner](https://cdn.discordapp.com/attachments/891939988088975372/931079377771450388/kirishima-ship-banner.png)

# @kirishima/template
# @kirishima/rest

</div>


# Features
- lorem
- ipsum
- 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);
})()
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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",
Expand All @@ -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"
}
}
98 changes: 98 additions & 0 deletions src/Structures/REST.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
await this.post<string>('routeplanner/free/address', { body: JSON.stringify({ address }) });
},
freeAllAddress: async (): Promise<void> => {
await this.post<string>('routeplanner/free/all');
},
status: async (): Promise<RoutePlannerStatusResponse> => {
return this.get<RoutePlannerStatusResponse>('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<LoadTrackResponse>(`loadtracks?identifier=${this.resolveIdentifier(LavalinkSourceEnum.Youtube)}:${options}`);
}
const source = options.source ?? LavalinkSourceEnum.Youtube;
const { query } = options;
return this.get<LoadTrackResponse>(
`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<LavalinkTrack[]>('decodetracks', {
body: JSON.stringify(trackOrTracks),
headers: { ...this.headers, 'Content-Type': 'application/json' }
});
}
return this.post<LavalinkTrack[]>('decodetracks', {
body: JSON.stringify([trackOrTracks]),
headers: { ...this.headers, 'Content-Type': 'application/json' }
});
}

private async get<T>(route: string, init?: RequestInit | undefined): Promise<T> {
await this.queue.wait();
try {
return fetch(`${this.url}/${route}`, { headers: this.headers, ...init }, FetchResultTypes.JSON);
} finally {
this.queue.shift();
}
}

private async post<T>(route: string, init?: RequestInit | undefined): Promise<T> {
await this.queue.wait();
try {
return fetch(`${this.url}/${route}`, { headers: this.headers, method: 'POST', ...init }, FetchResultTypes.JSON);
} finally {
this.queue.shift();
}
}
}
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const main = () => 'Kirishima ship ready to serve !';

export default main;
export { REST } from './Structures/REST';

0 comments on commit f1e2566

Please sign in to comment.