chore: init api tokens data store and model

This commit is contained in:
Aaryan Khandelwal 2024-03-05 15:32:16 +05:30
parent a43b23907d
commit b52f2487f4
5 changed files with 229 additions and 2 deletions

View File

@ -0,0 +1,110 @@
import { computed, makeObservable, observable } from "mobx";
import { DataStore } from "./dataMaps";
// services
import { APITokenService } from "services/api_token.service";
// types
import { IApiToken } from "@plane/types";
export interface IAPITokenModel {
// model observables
created_at: string;
created_by: string;
description: string;
expired_at: string | null;
id: string;
is_active: boolean;
label: string;
last_used: string | null;
updated_at: string;
updated_by: string;
user: string;
user_type: number;
token?: string;
workspace: string;
// computed
asJSON: IApiToken;
}
export class APIModel implements IAPITokenModel {
// model observables
created_at: string;
created_by: string;
description: string;
expired_at: string | null;
id: string;
is_active: boolean;
label: string;
last_used: string | null;
updated_at: string;
updated_by: string;
user: string;
user_type: number;
token?: string;
workspace: string;
// root store
dataStore;
// services
apiTokenService;
constructor(apiToken: IApiToken, _dataStore: DataStore) {
makeObservable(this, {
// model observables
created_at: observable.ref,
created_by: observable.ref,
description: observable.ref,
expired_at: observable.ref,
id: observable.ref,
is_active: observable.ref,
label: observable.ref,
last_used: observable.ref,
updated_at: observable.ref,
updated_by: observable.ref,
user: observable.ref,
user_type: observable.ref,
token: observable.ref,
workspace: observable.ref,
// computed
asJSON: computed,
});
this.dataStore = _dataStore;
// services
this.apiTokenService = new APITokenService();
this.created_at = apiToken.created_at;
this.created_by = apiToken.created_by;
this.description = apiToken.description;
this.expired_at = apiToken.expired_at;
this.id = apiToken.id;
this.is_active = apiToken.is_active;
this.label = apiToken.label;
this.last_used = apiToken.last_used;
this.updated_at = apiToken.updated_at;
this.updated_by = apiToken.updated_by;
this.user = apiToken.user;
this.user_type = apiToken.user_type;
this.token = apiToken.token;
this.workspace = apiToken.workspace;
}
/**
* @description returns the API token data in JSON format
*/
get asJSON() {
return {
created_at: this.created_at,
created_by: this.created_by,
description: this.description,
expired_at: this.expired_at,
id: this.id,
is_active: this.is_active,
label: this.label,
last_used: this.last_used,
updated_at: this.updated_at,
updated_by: this.updated_by,
user: this.user,
user_type: this.user_type,
token: this.token,
workspace: this.workspace,
};
}
}

View File

@ -0,0 +1,54 @@
import { action, makeObservable, observable } from "mobx";
import { computedFn } from "mobx-utils";
import { set } from "lodash";
// store
import { DataStore } from ".";
import { APIModel } from "store/api-token.store";
// types
import { IApiToken } from "@plane/types";
export interface IAPITokenData {
// observables
apiTokenMap: Record<string, APIModel>;
// actions
addAPIToken: (apiToken: IApiToken) => void;
deleteAPIToken: (apiTokenId: string) => void;
getAPITokenById: (apiTokenId: string) => APIModel | undefined;
}
export class APITokenData implements IAPITokenData {
// observables
apiTokenMap: Record<string, APIModel> = {};
// data store
dataStore;
constructor(_dataStore: DataStore) {
makeObservable(this, {
// observables
apiTokenMap: observable,
// actions
addAPIToken: action,
deleteAPIToken: action,
});
this.dataStore = _dataStore;
}
/**
* @description add a API token to the API token map
* @param {IApiToken} apiToken
*/
addAPIToken = (apiToken: IApiToken) => set(this.apiTokenMap, [apiToken.id], new APIModel(apiToken, this.dataStore));
/**
* @description delete a API token from the API token map
* @param {string} apiTokenId
*/
deleteAPIToken = (apiTokenId: string) => delete this.apiTokenMap[apiTokenId];
/**
* @description get a API token model by its id
* @param {string} apiTokenId
* @returns {APIModel | undefined} API token model
*/
getAPITokenById = computedFn((apiTokenId: string) => this.apiTokenMap[apiTokenId]);
}

View File

@ -1,13 +1,16 @@
import { APITokenData, IAPITokenData } from "./api-token.data.store";
import { IWebhookData, WebhookData } from "./webhook.data.store";
import { IWorkspaceData, WorkspaceData } from "./workspace.data.store";
export class DataStore {
workspace: IWorkspaceData;
webhook: IWebhookData;
apiToken: IAPITokenData;
constructor() {
this.workspace = new WorkspaceData(this);
this.webhook = new WebhookData(this);
this.apiToken = new APITokenData(this);
}
resetOnSignOut() {}

View File

@ -46,12 +46,16 @@ export class WebhookModel implements IWebhookModel {
constructor(webhook: IWebhook, _dataStore: DataStore) {
makeObservable(this, {
// model observables
created_at: observable.ref,
cycle: observable.ref,
id: observable.ref,
is_active: observable.ref,
issue: observable.ref,
issue_comment: observable.ref,
module: observable.ref,
project: observable.ref,
secret_key: observable.ref,
updated_at: observable.ref,
url: observable.ref,
// computed
asJSON: computed,

View File

@ -4,9 +4,11 @@ import { DataStore } from "./dataMaps";
// services
import { WorkspaceService } from "services/workspace.service";
import { WebhookService } from "services/webhook.service";
import { APITokenService } from "services/api_token.service";
// types
import { IUser, IWebhook, IWorkspace } from "@plane/types";
import { IApiToken, IUser, IWebhook, IWorkspace } from "@plane/types";
import { IWebhookModel } from "./webhook.store";
import { IAPITokenModel } from "./api-token.store";
export interface IWorkspaceModel {
// model observables
@ -25,6 +27,7 @@ export interface IWorkspaceModel {
updated_by: string;
// data maps
webhooks: Record<string, IWebhookModel>;
apiTokens: Record<string, IAPITokenModel>;
// computed
asJSON: IWorkspace;
// workspace actions
@ -33,6 +36,10 @@ export interface IWorkspaceModel {
fetchWebhooks: () => Promise<IWebhook[]>;
createWebhook: (data: Partial<IWebhook>) => Promise<IWebhook>;
deleteWebhook: (webhookId: string) => Promise<void>;
// API token actions
fetchApiTokens: () => Promise<IApiToken[]>;
createApiToken: (data: Partial<IApiToken>) => Promise<IApiToken>;
deleteApiToken: (tokenId: string) => Promise<void>;
}
export class WorkspaceModel implements IWorkspaceModel {
@ -52,11 +59,13 @@ export class WorkspaceModel implements IWorkspaceModel {
url: string;
// data maps
webhooks: Record<string, IWebhookModel> = {};
apiTokens: Record<string, IAPITokenModel> = {};
// root store
dataStore;
// services
workspaceService;
webhookService;
apiTokenService;
constructor(workspace: IWorkspace, _dataStore: DataStore) {
makeObservable(this, {
@ -76,6 +85,7 @@ export class WorkspaceModel implements IWorkspaceModel {
url: observable.ref,
// data maps
webhooks: observable,
apiTokens: observable,
// computed
asJSON: computed,
// workspace actions
@ -84,11 +94,16 @@ export class WorkspaceModel implements IWorkspaceModel {
fetchWebhooks: action,
createWebhook: action,
deleteWebhook: action,
// API token actions
fetchApiTokens: action,
createApiToken: action,
deleteApiToken: action,
});
this.dataStore = _dataStore;
// services
this.workspaceService = new WorkspaceService();
this.webhookService = new WebhookService();
this.apiTokenService = new APITokenService();
this.created_at = workspace.created_at;
this.created_by = workspace.created_by;
@ -167,7 +182,6 @@ export class WorkspaceModel implements IWorkspaceModel {
set(this.webhooks, [webhook.id], this.dataStore.webhook.webhookMap[webhook.id]);
});
});
return webhooksResponse;
};
@ -195,4 +209,46 @@ export class WorkspaceModel implements IWorkspaceModel {
delete this.webhooks[webhookId];
});
});
// API token actions
/**
* @description fetch all the API tokens of the workspace
*/
fetchApiTokens = async () => {
const apiTokensResponse = await this.apiTokenService.getApiTokens(this.slug);
runInAction(() => {
apiTokensResponse.forEach((apiToken) => {
this.dataStore.apiToken.addAPIToken(apiToken);
set(this.apiTokens, [apiToken.id], this.dataStore.apiToken.apiTokenMap[apiToken.id]);
});
});
return apiTokensResponse;
};
/**
* @description create API token using data
* @param {Partial<IApiToken>} data
*/
createApiToken = async (data: Partial<IApiToken>) =>
await this.apiTokenService.createApiToken(this.slug, data).then((response) => {
runInAction(() => {
this.dataStore.apiToken.addAPIToken(response);
set(this.apiTokens, [response.id], this.dataStore.apiToken.apiTokenMap[response.id]);
});
return response;
});
/**
* @description delete API token using token id
* @param {string} tokenId
*/
deleteApiToken = async (tokenId: string) =>
await this.apiTokenService.deleteApiToken(this.slug, tokenId).then(() => {
this.dataStore.apiToken.deleteAPIToken(tokenId);
runInAction(() => {
delete this.apiTokens[tokenId];
});
});
}