update ts client

This commit is contained in:
kv
2024-01-13 13:40:14 -08:00
parent bbca105077
commit 78cefe19bd
3 changed files with 86 additions and 112 deletions

View File

@@ -14,83 +14,39 @@ import {
export * as Types from "./types";
export type LoginOptions = {
jwt?: string;
username?: string;
password?: string;
secret?: string;
};
type InitOptions =
| { type: "jwt"; params: { jwt: string } }
| { type: "api-key"; params: { api_key: string; secret: string } };
export function MonitorClient(base_url: string, token?: string) {
const state = { jwt: token ?? "" };
const auth = async <R extends AuthRequest>(
request: R
): Promise<AuthResponses[R["type"]]> => {
return await axios({
method: "post",
url: base_url + "/auth",
data: request,
}).then(({ data }) => data);
export function MonitorClient(url: string, options: InitOptions) {
const state = {
jwt: options.type === "jwt" ? options.params.jwt : undefined,
api_key: options.type === "api-key" ? options.params.api_key : undefined,
secret: options.type === "api-key" ? options.params.secret : undefined,
};
const login = async (options: LoginOptions) => {
if (options.username) {
if (options.password) {
const res = await auth({
type: "LoginLocalUser",
params: { username: options.username, password: options.password },
});
state.jwt = res.jwt;
return res.jwt;
} else if (options.secret) {
const { jwt } = await auth({
type: "LoginWithSecret",
params: { username: options.username, secret: options.secret },
});
state.jwt = jwt;
return jwt;
}
}
};
const request = async <Req, Res>(path: string, request: Req) =>
await axios
.post<Res>(url + path, request, {
headers: {
Authorization: state.jwt,
"X-API-KEY": state.api_key,
"X-API-SECRET": state.secret,
},
})
.then(({ data }) => data);
const request = async <Req, Res>(
path: string,
request: Req
): Promise<Res> => {
return await axios({
method: "post",
url: base_url + path,
headers: {
Authorization: `Bearer ${state.jwt}`,
},
data: request,
}).then(({ data }) => data);
};
const auth = async <Req extends AuthRequest>(req: Req) =>
await request<Req, AuthResponses[Req["type"]]>("/auth", req);
const read = async <R extends ReadRequest>(
req: R
): Promise<ReadResponses[R["type"]]> => {
return await request("/read", req);
};
const read = async <Req extends ReadRequest>(req: Req) =>
await request<Req, ReadResponses[Req["type"]]>("/read", req);
const write = async <R extends WriteRequest>(
req: R
): Promise<WriteResponses[R["type"]]> => {
return await request("/write", req);
};
const write = async <Req extends WriteRequest>(req: Req) =>
await request<Req, WriteResponses[Req["type"]]>("/write", req);
const execute = async <R extends ExecuteRequest>(
req: R
): Promise<ExecuteResponses[R["type"]]> => {
return await request("/execute", req);
};
const execute = async <Req extends ExecuteRequest>(req: Req) =>
await request<Req, ExecuteResponses[Req["type"]]>("/execute", req);
return {
auth,
login,
read,
write,
execute,
};
return { request, auth, read, write, execute };
}

View File

@@ -5,7 +5,6 @@ export type AuthResponses = {
CreateLocalUser: Types.CreateLocalUserResponse;
LoginLocalUser: Types.LoginLocalUserResponse;
ExchangeForJwt: Types.ExchangeForJwtResponse;
LoginWithSecret: Types.LoginWithSecretResponse;
};
export type ReadResponses = {
@@ -14,6 +13,7 @@ export type ReadResponses = {
GetUsers: Types.GetUsersResponse;
GetUsername: Types.GetUsernameResponse;
GetCoreInfo: Types.GetCoreInfoResponse;
ListApiKeys: Types.ListApiKeysResponse;
// ==== SEARCH ====
FindResources: Types.FindResourcesResponse;
@@ -97,9 +97,9 @@ export type ReadResponses = {
};
export type WriteResponses = {
// ==== SECRET ====
CreateLoginSecret: Types.CreateLoginSecretResponse;
DeleteLoginSecret: Types.DeleteLoginSecretResponse;
// ==== API KEY ====
CreateApiKey: Types.CreateApiKeyResponse;
DeleteApiKey: Types.DeleteApiKeyResponse;
// ==== USER ====
PushRecentlyViewed: Types.PushRecentlyViewedResponse;

View File

@@ -1,5 +1,5 @@
/*
Generated by typeshare 1.6.0
Generated by typeshare 1.7.0
*/
export interface MongoIdObj {
@@ -257,13 +257,6 @@ export interface DeploymentActionState {
export type GetDeploymentActionStateResponse = DeploymentActionState;
export interface ApiSecret {
name: string;
hash?: string;
created_at: I64;
expires?: I64;
}
export type ResourceTarget =
| { type: "System", id: string }
| { type: "Build", id: string }
@@ -282,7 +275,6 @@ export interface User {
create_server_permissions?: boolean;
create_build_permissions?: boolean;
avatar?: string;
secrets?: ApiSecret[];
password?: string;
github_id?: string;
google_id?: string;
@@ -293,6 +285,23 @@ export interface User {
export type GetUserResponse = User;
export interface ApiKey {
/** UNIQUE KEY ASSOCIATED WITH SECRET */
key: string;
/** HASH OF THE SECRET */
secret: string;
/** USER ASSOCIATED WITH THE API KEY */
user_id: string;
/** NAME ASSOCIATED WITH THE API KEY FOR MANAGEMENT */
name: string;
/** TIMESTAMP OF KEY CREATION */
created_at: I64;
/** EXPIRY OF KEY, OR 0 IF NEVER EXPIRES */
expires: I64;
}
export type ListApiKeysResponse = ApiKey[];
export type GetUsersResponse = User[];
export type ProcedureConfig =
@@ -771,15 +780,6 @@ export interface ExchangeForJwtResponse {
jwt: string;
}
export interface LoginWithSecret {
username: string;
secret: string;
}
export interface LoginWithSecretResponse {
jwt: string;
}
export interface RunBuild {
build_id: string;
}
@@ -1060,6 +1060,9 @@ export interface GetVersionResponse {
export interface GetUser {
}
export interface ListApiKeys {
}
export interface GetUsers {
}
@@ -1308,6 +1311,28 @@ export interface UpdateAlerter {
config: PartialAlerterConfig;
}
export interface CreateApiKey {
name: string;
expires?: I64;
}
export interface CreateApiKeyResponse {
/** X-API-KEY */
key: string;
/**
* X-API-SECRET
* There is no way to get the secret again after it is distributed in this message
*/
secret: string;
}
export interface DeleteApiKey {
key: string;
}
export interface DeleteApiKeyResponse {
}
export interface CreateBuild {
name: string;
config: _PartialBuildConfig;
@@ -1460,22 +1485,6 @@ export interface UpdateRepo {
config: _PartialRepoConfig;
}
export interface CreateLoginSecret {
name: string;
expires?: I64;
}
export interface CreateLoginSecretResponse {
secret: string;
}
export interface DeleteLoginSecret {
name: string;
}
export interface DeleteLoginSecretResponse {
}
export interface CreateServer {
name: string;
config: _PartialServerConfig;
@@ -1589,7 +1598,6 @@ export type AuthRequest =
| { type: "GetLoginOptions", params: GetLoginOptions }
| { type: "CreateLocalUser", params: CreateLocalUser }
| { type: "LoginLocalUser", params: LoginLocalUser }
| { type: "LoginWithSecret", params: LoginWithSecret }
| { type: "ExchangeForJwt", params: ExchangeForJwt };
export type ExecuteRequest =
@@ -1612,6 +1620,7 @@ export type ReadRequest =
| { type: "GetUsers", params: GetUsers }
| { type: "GetUsername", params: GetUsername }
| { type: "GetCoreInfo", params: GetCoreInfo }
| { type: "ListApiKeys", params: ListApiKeys }
| { type: "FindResources", params: FindResources }
| { type: "GetProceduresSummary", params: GetProceduresSummary }
| { type: "GetProcedure", params: GetProcedure }
@@ -1670,8 +1679,8 @@ export type ReadRequest =
| { type: "GetSystemComponents", params: GetSystemComponents };
export type WriteRequest =
| { type: "CreateLoginSecret", params: CreateLoginSecret }
| { type: "DeleteLoginSecret", params: DeleteLoginSecret }
| { type: "CreateApiKey", params: CreateApiKey }
| { type: "DeleteApiKey", params: DeleteApiKey }
| { type: "PushRecentlyViewed", params: PushRecentlyViewed }
| { type: "SetLastSeenUpdate", params: SetLastSeenUpdate }
| { type: "UpdateUserPerimissions", params: UpdateUserPermissions }
@@ -1740,3 +1749,12 @@ export type Tag =
tag_id: string;
}};
export type WsLoginMessage =
| { type: "Jwt", params: {
jwt: string;
}}
| { type: "ApiKeys", params: {
key: string;
secret: string;
}};