feat: implement API token management and project settings methods
This commit is contained in:
107
src/index.ts
107
src/index.ts
@ -1,7 +1,104 @@
|
||||
export class FewLineSDK {
|
||||
constructor(private url: string, private token: string) {}
|
||||
import axios from "axios";
|
||||
import { GenerateTokenResponse } from "./types/token.type";
|
||||
|
||||
public getUrl(): string {
|
||||
return this.url;
|
||||
}
|
||||
export class FewLineSDK {
|
||||
constructor(private url: string, private token: string) {}
|
||||
|
||||
public getUrl(): string {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
private async createRequestWithAuthHeaders(
|
||||
url: string,
|
||||
method: string,
|
||||
data?: any,
|
||||
headers: any = {}
|
||||
): Promise<any> {
|
||||
return await axios.request({
|
||||
url,
|
||||
method,
|
||||
data,
|
||||
headers: {
|
||||
"x-api-token": this.token,
|
||||
...headers,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* API Tokens part
|
||||
*/
|
||||
async generateToken(projectId: string): Promise<GenerateTokenResponse> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/api/token/generate`,
|
||||
"POST",
|
||||
{
|
||||
id: projectId,
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async revokeToken(token: string): Promise<GenerateTokenResponse> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/api/token/revoke/${token}`,
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects management part
|
||||
*/
|
||||
async createProject(name: string, withDb: boolean = true): Promise<any> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/project/${withDb ? "create" : "create-without-db"}`,
|
||||
"PUT",
|
||||
{
|
||||
name,
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async createSettings(setting: { key: string; value: string }): Promise<any> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/project/settings/create`,
|
||||
"PUT",
|
||||
setting
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async deleteSetting(key: string): Promise<any> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/project/settings/delete`,
|
||||
"DELETE",
|
||||
{ key }
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async getAllSettings(): Promise<any> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/project/settings`,
|
||||
"GET"
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
|
||||
async getAllApiTokens(): Promise<any> {
|
||||
const response = await this.createRequestWithAuthHeaders(
|
||||
`${this.url}/project/api-tokens`,
|
||||
"GET"
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
5
src/types/token.type.ts
Normal file
5
src/types/token.type.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export type GenerateTokenResponse = {
|
||||
token: string;
|
||||
isActive: boolean;
|
||||
isAdmin: boolean;
|
||||
};
|
||||
Reference in New Issue
Block a user