Added certificate model & improvements

This commit is contained in:
Filip Znachor 2024-06-28 11:46:38 +02:00
parent 3d4c04fcde
commit 639bfc2ade
8 changed files with 90 additions and 7 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
lib

View file

@ -1,8 +1,7 @@
{
"name": "oohost.js",
"version": "0.0.1",
"version": "0.0.2",
"description": "TypeScript library for accessing the oohost API",
"main": "src/index.ts",
"license": "MIT",
"author": "oohost.cz",
"repository": {
@ -13,5 +12,11 @@
"oohost",
"webhosting",
"api"
]
],
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"devDependencies": {
"@types/node": "^20.14.9",
"typescript": "^5.5.2"
}
}

View file

@ -1,7 +1,7 @@
/**
* Class for requesting API
*/
export default class API {
export class API {
/**
* Default API endpoint

View file

@ -1,3 +1,4 @@
import API from "./API";
export { API };
export * from "./API";
export * from "./models/Base";
export * from "./models/User";
export * from "./models/Certificate";

4
src/models/Base.ts Normal file
View file

@ -0,0 +1,4 @@
import { Response } from "../API";
export type PromisedData<T> = Promise<Data<T>>;
export type Data<T> = [T | null, Response.ExtendedError[]];

53
src/models/Certificate.ts Normal file
View file

@ -0,0 +1,53 @@
import { API, Response } from "../API";
import { PromisedData } from "./Base";
import { UserInclude } from "./User";
export class Certificate {
public readonly id: number;
constructor(id: number) {
this.id = id;
}
public async getDetails(): PromisedData<CertificateDetails> {
let r = await API.get("certs/details", {id: this.id});
return [ r.get().details ?? null, r.get_errors() ];
}
public async remove(): Promise<Response> {
return await API.get("certs/remove", {id: this.id});
}
public async generate(domains: string): Promise<Response> {
return await API.get("certs/generate", {id: this.id, domains});
}
public async edit(data: CertificateEdit): Promise<Response> {
return await API.get("certs/edit", {id: this.id, ...data});
}
}
export interface CertificateEdit {
name?: string,
private_key?: string,
certificate?: string
}
export interface CertificateListItem {
id: number,
name: string,
domain: string,
auto_renew: boolean,
expiration_date: string,
created_at: string,
updated_at: string,
user_id: number,
User: UserInclude
}
export interface CertificateDetails extends CertificateListItem {
private_key: string,
certificate: string
}

6
src/models/User.ts Normal file
View file

@ -0,0 +1,6 @@
export interface UserInclude {
id: number,
username: string,
name: string,
email: string
}

12
tsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "esnext",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"outDir": "./lib"
},
"include": ["src"]
}