Skip to content

Commit

Permalink
feat(client): add mangaers to class
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Nov 1, 2021
1 parent 63af020 commit f22b3f2
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/lib/client/BaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Imperial } from "./Imperial";
import type { Imperial } from ".";

export abstract class BaseClient {
constructor(client: Imperial) {
Expand Down
95 changes: 0 additions & 95 deletions src/lib/client/Imperial.ts

This file was deleted.

110 changes: 109 additions & 1 deletion src/lib/client/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
export { Imperial } from "./Imperial";
import type { ImperialOptions } from "../types/common";
import { validateToken } from "../utils/validToken";
import { Rest } from "../rest/Rest";
import { defaultOptions } from "../utils/defaultOptions";
import { DocumentManager } from "../managers/Document";
import { UsersManager } from "../managers/Users";
import { MeManager } from "../managers/Me";
import { ErrorMessage } from "../errors/Messages";

/**
* The Imperial class
* Easiest way to interact with the Api
* @author `pxseu` <https://github.com/pxseu>
*/
export class Imperial {
/**
* `Imperial` constructor
* @param token Your API token
*/
constructor(token?: string | null);

/**
* `Imperial` constructor
* @param options Options for the class
*/
constructor(options?: ImperialOptions);

/**
* `Imperial` constructor
* @param token Your API token
* @param options Options for the class
*/
constructor(token?: string, options?: ImperialOptions);

constructor(tokenOrOptions?: ImperialOptions | string | null, paramOptions: ImperialOptions = {}) {
const options = typeof tokenOrOptions === "object" && tokenOrOptions !== null ? tokenOrOptions : paramOptions;
const token = typeof tokenOrOptions === "string" || tokenOrOptions === null ? tokenOrOptions : undefined;

this.setApiToken(token);
this.options = { ...defaultOptions, ...options };

Object.defineProperty(this, "rest", { value: new Rest(this) });
Object.defineProperty(this, "document", { value: new DocumentManager(this) });
Object.defineProperty(this, "users", { value: new UsersManager(this) });
Object.defineProperty(this, "me", { value: new MeManager(this) });
}

/**
* Get the Api Token from the Class
*/
public get apiToken(): string | undefined {
return this._token;
}

/**
* Change the Api Token on the Class
*
*/
public setApiToken(token: string | null | undefined = null): void {
if (validateToken(token)) Object.defineProperty(this, "_token", { value: token, configurable: true });
}

/**
* Verify if your token is valid | **Requires an API Token**
* @example verify().then(console.log)
* // shows if the token is valid
*/
public async verify(): Promise<void> {
// If no token return
if (!this.apiToken) throw new Error(ErrorMessage("NO_TOKEN"));

await this.rest.request("GET", `/checkApiToken/${encodeURIComponent(this.apiToken)}`);
}
}

export interface Imperial {
/**
* The token
* @internal
*/
_token: string | undefined;

/**
* Main Class to handle interactions with the REST Api
* @internal
*/
readonly rest: Rest;

/**
* Client options
* @internal
*/
options: ImperialOptions;

/**
* Document Manager
*/
readonly document: DocumentManager;

/**
* Users Manager
*/
readonly users: UsersManager;

/**
* User Manager
*/
readonly me: MeManager;
}

0 comments on commit f22b3f2

Please sign in to comment.