changing javascript to typescript

This commit is contained in:
night0721 2021-09-13 19:25:00 +08:00
parent 08e984b00b
commit f564e8bffd
13 changed files with 92 additions and 42 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
dist
# Packages # Packages
node_modules/ node_modules/
package-lock.json package-lock.json

View file

@ -1,3 +1,5 @@
src
tsconfig.json
# Packages # Packages
node_modules/ node_modules/
package-lock.json package-lock.json

View file

@ -1,3 +0,0 @@
const APIClient = require("./src/class/apiclient");
const random8ball = require("./src/functions/8ball");
module.exports = { random8ball, APIClient };

View file

@ -1,11 +1,13 @@
{ {
"name": "cath", "name": "cath.js",
"version": "1.1.0", "version": "1.1.0",
"description": "A powerful package that can interact with Cath API", "description": "A powerful package that can interact with Cath API",
"main": "index.js", "main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon .", "dev": "nodemon .",
"build": "tsc",
"start": "node .", "start": "node .",
"node-update": "npm i --save-dev node@16 && npm config set prefix=$(pwd)/node_modules/node && export PATH=$(pwd)/node_modules/node/bin:$PATH", "node-update": "npm i --save-dev node@16 && npm config set prefix=$(pwd)/node_modules/node && export PATH=$(pwd)/node_modules/node/bin:$PATH",
"node-clean": "rm -rf node_modules && rm package-lock.json && npm cache clear --force && npm cache clean --force && npm i", "node-clean": "rm -rf node_modules && rm package-lock.json && npm cache clear --force && npm cache clean --force && npm i",
@ -34,5 +36,11 @@
"dependencies": { "dependencies": {
"axios": "^0.21.4", "axios": "^0.21.4",
"discord.js": "^13.1.0" "discord.js": "^13.1.0"
},
"devDependencies": {
"@types/jsdom": "^16.2.13",
"ts-node": "^10.2.1",
"typedoc": "^0.21.9",
"typescript": "^4.4.3"
} }
} }

View file

@ -0,0 +1,6 @@
export interface CODMClientOptions {
/**
* Authorisation key for the API
*/
key: string;
}

View file

@ -1,26 +1,23 @@
const axios = require("axios"); import axios from "axios";
const config = require("../utils/config.json"); import config from "../utils/config.json";
import { CODMClientOptions } from "./codmclient.interface";
/** /**
* @class APIClient
**/
class APIClient {
/**
* @name APIClient * @name APIClient
* @kind constructor * @kind constructor
* @param {String} key Authorization Key for API (Only for CODM commands) * @param {String} key Authorization Key for API (Only for CODM commands)
* @param {String} [options.key]
*/ */
constructor(key, options = {}) { export class CODMClient {
public key: CODMClientOptions;
constructor(key: CODMClientOptions) {
if (key && typeof key !== "string") if (key && typeof key !== "string")
throw new TypeError("API key must be a string"); throw new TypeError("API key must be a string");
if (key) this.key = key;
} }
/** /**
* Sends a CODM perk object * Sends a CODM perk object
* @returns {Promise<Object>} * @return {Promise<Object>}
* @param {String} name * @param {String} name
*/ */
async getperk(name) { public async getperk(name: string): Promise<object> {
const data = await axios const data = await axios
.get(`${config.api}/api/v1/codm/perks?name=${name}`, { .get(`${config.api}/api/v1/codm/perks?name=${name}`, {
headers: { headers: {
@ -32,4 +29,3 @@ class APIClient {
return data; return data;
} }
} }
module.exports = APIClient;

2
src/CODMClient/index.ts Normal file
View file

@ -0,0 +1,2 @@
export { CODMClientOptions } from "./codmclient.interface";
export { CODMClient } from "./codmclient";

View file

@ -1,13 +0,0 @@
const axios = require("axios");
const config = require("../utils/config.json");
/**
* Sends a 8ball response
* @returns {Promise<String>}
*/
async function random() {
const data = await axios
.get(`${config.api}/api/v1/fun/8ball`)
.then(res => res.data);
return data.answer;
}
module.exports = random;

12
src/functions/8ball.ts Normal file
View file

@ -0,0 +1,12 @@
import axios from "axios";
import config from "../utils/config.json";
/**
* Sends a 8ball response
* @return {Promise<String>}
*/
export async function random8ball(): Promise<string> {
const data = await axios
.get(`${config.api}/api/v1/fun/8ball`)
.then(res => res.data);
return data.answer;
}

2
src/index.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
export { CODMClient, CODMClientOptions } from "./CODMClient/index";
export { random8ball } from "./functions/8ball";

20
src/index.js Normal file
View file

@ -0,0 +1,20 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CODMClient = void 0;
// CODMClient
var codmclient_1 = require("./CODMClient/index");
Object.defineProperty(exports, "CODMClient", {
enumerable: true,
get: function () {
return codmclient_1.CODMClient;
},
});
// functions
var _8ball_1 = require("./functions/8ball");
Object.defineProperty(exports, "random8ball", {
enumerable: true,
get: function () {
return _8ball_1.random8ball;
},
});
//# sourceMappingURL=index.js.map

View file

@ -1,14 +1,11 @@
import { CODMClientOptions } from "../CODMClient";
declare module "cath" { declare module "cath" {
export class APIClient { export class CODMClient {
public constructor(key: string, options?: APIClientOptions); public constructor(key: CODMClientOptions);
private key: string; private key: string;
public options?: APIClientOptions;
public random8ball(): Promise<String>; public getperk(perk: string): Promise<object>;
public getperk(perk: string): Promise<Object>;
} }
export type APIClientOptions = { export async function random8ball(): Promise<string>;
codm?: string;
};
} }

20
tsconfig.json Normal file
View file

@ -0,0 +1,20 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "commonjs",
"moduleResolution": "node",
"target": "ESNext",
"declaration": true,
"outDir": "dist",
"sourceMap": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"resolveJsonModule": true
},
"include": ["./src"],
"exclude": ["./node_modules"]
}