Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-3066] refactor: replace Space Services with Services Package #6353

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/services/src/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ export abstract class APIService {
(error) => {
if (error.response && error.response.status === 401) {
const currentPath = window.location.pathname;
window.location.replace(`/${currentPath ? `?next_path=${currentPath}` : ``}`);
let prefix = "";
let updatedPath = currentPath;

// Check for special path prefixes
if (currentPath.startsWith("/god-mode")) {
prefix = "/god-mode";
updatedPath = currentPath.replace("/god-mode", "");
} else if (currentPath.startsWith("/spaces")) {
prefix = "/spaces";
updatedPath = currentPath.replace("/spaces", "");
}

window.location.replace(`${prefix}${updatedPath ? `?next_path=${updatedPath}` : ""}`);
}
return Promise.reject(error);
}
Expand Down
1 change: 1 addition & 0 deletions packages/services/src/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./auth.service";
export * from "./sites-auth.service";
49 changes: 49 additions & 0 deletions packages/services/src/auth/sites-auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { API_BASE_URL } from "@plane/constants";
// types
import { IEmailCheckData, IEmailCheckResponse } from "@plane/types";
// services
import { APIService } from "../api.service";

/**
* Service class for handling authentication-related operations for Plane space application
* Provides methods for user authentication, password management, and session handling
* @extends {APIService}
* @remarks This service is only available for plane sites
*/
export class SitesAuthService extends APIService {
/**
* Creates an instance of SitesAuthService
* Initializes with the base API URL
*/
constructor(BASE_URL?: string) {
super(BASE_URL || API_BASE_URL);
}

/**
* Checks if an email exists in the system
* @param {IEmailCheckData} data - Email data to verify
* @returns {Promise<IEmailCheckResponse>} Response indicating email status
* @throws {Error} Throws response data if the request fails
*/
async emailCheck(data: IEmailCheckData): Promise<IEmailCheckResponse> {
return this.post("/auth/spaces/email-check/", data, { headers: {} })
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}

/**
* Generates a unique code for magic link authentication
* @param {{ email: string }} data - Object containing the email address
* @returns {Promise<any>} Response containing the generated unique code
* @throws {Error} Throws response data if the request fails
*/
async generateUniqueCode(data: { email: string }): Promise<any> {
return this.post("/auth/spaces/magic-generate/", data, { headers: {} })
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}
prateekshourya29 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/services/src/cycle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./cycle-analytics.service";
export * from "./cycle-archive.service";
export * from "./cycle-operations.service";
export * from "./cycle.service";
export * from "./sites-cycle.service";
31 changes: 31 additions & 0 deletions packages/services/src/cycle/sites-cycle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// plane imports
import { API_BASE_URL } from "@plane/constants";
import { TPublicCycle } from "@plane/types";
// api service
import { APIService } from "../api.service";

/**
* Service class for managing cycles within plane sites application.
* Extends APIService to handle HTTP requests to the cycle-related endpoints.
* @extends {APIService}
* @remarks This service is only available for plane sites
*/
export class SitesCycleService extends APIService {
constructor(BASE_URL?: string) {
super(BASE_URL || API_BASE_URL);
}

/**
* Retrieves list of cycles for a specific anchor.
* @param anchor - The anchor identifier for the published entity
* @returns {Promise<TPublicCycle[]>} The list of cycles
* @throws {Error} If the request fails
*/
async list(anchor: string): Promise<TPublicCycle[]> {
return this.get(`/api/public/anchor/${anchor}/cycles/`)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import axios from "axios";
// services
import { APIService } from "@/services/api.service";
// api service
import { APIService } from "../api.service";

/**
* Service class for handling file upload operations
* Handles file uploads
* @extends {APIService}
*/
export class FileUploadService extends APIService {
private cancelSource: any;

constructor() {
super("");
}

async uploadFile(url: string, data: FormData): Promise<void> {
/**
* Uploads a file to the specified signed URL
* @param {string} url - The URL to upload the file to
* @param {FormData} data - The form data to upload
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async uploadFile(
url: string,
data: FormData,
): Promise<void> {
this.cancelSource = axios.CancelToken.source();
return this.post(url, data, {
headers: {
Expand All @@ -28,7 +43,10 @@ export class FileUploadService extends APIService {
});
}

/**
* Cancels the upload
*/
cancelUpload() {
this.cancelSource.cancel("Upload canceled");
}
}
}
67 changes: 67 additions & 0 deletions packages/services/src/file/file.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// plane imports
import { API_BASE_URL } from "@plane/constants";
// api service
import { APIService } from "../api.service";
// helpers
import { getAssetIdFromUrl } from "./helper";

/**
* Service class for managing file operations within plane applications.
* Extends APIService to handle HTTP requests to the file-related endpoints.
* @extends {APIService}
*/
export class FileService extends APIService {
/**
* Creates an instance of FileService
* @param {string} BASE_URL - The base URL for API requests
*/
constructor(BASE_URL?: string) {
super(BASE_URL || API_BASE_URL);
}

/**
* Deletes a new asset
* @param {string} assetPath - The asset path
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async deleteNewAsset(assetPath: string): Promise<void> {
return this.delete(assetPath)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}

/**
* Deletes an old editor asset
* @param {string} workspaceId - The workspace identifier
* @param {string} src - The asset source
* @returns {Promise<any>} Promise resolving to void
* @throws {Error} If the request fails
*/
async deleteOldEditorAsset(workspaceId: string, src: string): Promise<any> {
const assetKey = getAssetIdFromUrl(src);
return this.delete(`/api/workspaces/file-assets/${workspaceId}/${assetKey}/`)
.then((response) => response?.status)
.catch((error) => {
throw error?.response?.data;
});
}

/**
* Restores an old editor asset
* @param {string} workspaceId - The workspace identifier
* @param {string} src - The asset source
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async restoreOldEditorAsset(workspaceId: string, src: string): Promise<void> {
const assetKey = getAssetIdFromUrl(src);
return this.post(`/api/workspaces/file-assets/${workspaceId}/${assetKey}/restore/`)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}
}
36 changes: 36 additions & 0 deletions packages/services/src/file/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { TFileMetaDataLite, TFileSignedURLResponse } from "@plane/types";

/**
* @description from the provided signed URL response, generate a payload to be used to upload the file
* @param {TFileSignedURLResponse} signedURLResponse
* @param {File} file
* @returns {FormData} file upload request payload
*/
export const generateFileUploadPayload = (signedURLResponse: TFileSignedURLResponse, file: File): FormData => {
const formData = new FormData();
Object.entries(signedURLResponse.upload_data.fields).forEach(([key, value]) => formData.append(key, value));
formData.append("file", file);
return formData;
};

/**
* @description returns the necessary file meta data to upload a file
* @param {File} file
* @returns {TFileMetaDataLite} payload with file info
*/
export const getFileMetaDataForUpload = (file: File): TFileMetaDataLite => ({
name: file.name,
size: file.size,
type: file.type,
});

/**
* @description this function returns the assetId from the asset source
* @param {string} src
* @returns {string} assetId
*/
export const getAssetIdFromUrl = (src: string): string => {
const sourcePaths = src.split("/");
const assetUrl = sourcePaths[sourcePaths.length - 1];
return assetUrl;
};
3 changes: 3 additions & 0 deletions packages/services/src/file/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./file-upload.service";
export * from "./sites-file.service";
export * from "./file.service";
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
// plane imports
import { API_BASE_URL } from "@plane/constants";
// local services
import { TFileEntityInfo, TFileSignedURLResponse } from "@plane/types";
import { FileUploadService } from "./file-upload.service";
// helpers
import { generateFileUploadPayload, getAssetIdFromUrl, getFileMetaDataForUpload } from "@/helpers/file.helper";
// services
import { APIService } from "@/services/api.service";
import { FileUploadService } from "@/services/file-upload.service";
import { FileService } from "./file.service";
import { generateFileUploadPayload, getAssetIdFromUrl, getFileMetaDataForUpload } from "./helper";

export class FileService extends APIService {
/**
* Service class for managing file operations within plane sites application.
* Extends FileService to manage file-related operations.
* @extends {FileService}
* @remarks This service is only available for plane sites
*/
export class SitesFileService extends FileService {
private cancelSource: any;
fileUploadService: FileUploadService;

prateekshourya29 marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
super(API_BASE_URL);
/**
* Creates an instance of SitesFileService
* @param {string} BASE_URL - The base URL for API requests
*/
constructor(BASE_URL?: string) {
super(BASE_URL || API_BASE_URL);
this.cancelUpload = this.cancelUpload.bind(this);
// services
this.fileUploadService = new FileUploadService();
}

/**
* Updates the upload status of an asset
* @param {string} anchor - The anchor identifier
* @param {string} assetId - The asset identifier
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
private async updateAssetUploadStatus(anchor: string, assetId: string): Promise<void> {
return this.patch(`/api/public/assets/v2/anchor/${anchor}/${assetId}/`)
.then((response) => response?.data)
Expand All @@ -25,6 +43,14 @@ export class FileService extends APIService {
});
}

/**
* Updates the upload status of multiple assets
* @param {string} anchor - The anchor identifier
* @param {string} entityId - The entity identifier
* @param {Object} data - The data payload
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async updateBulkAssetsUploadStatus(
anchor: string,
entityId: string,
Expand All @@ -39,6 +65,14 @@ export class FileService extends APIService {
});
}

/**
* Uploads a file to the specified anchor
* @param {string} anchor - The anchor identifier
* @param {TFileEntityInfo} data - The data payload
* @param {File} file - The file to upload
* @returns {Promise<TFileSignedURLResponse>} Promise resolving to the signed URL response
* @throws {Error} If the request fails
*/
async uploadAsset(anchor: string, data: TFileEntityInfo, file: File): Promise<TFileSignedURLResponse> {
const fileMetaData = getFileMetaDataForUpload(file);
return this.post(`/api/public/assets/v2/anchor/${anchor}/`, {
Expand All @@ -57,23 +91,13 @@ export class FileService extends APIService {
});
}

async deleteNewAsset(assetPath: string): Promise<void> {
return this.delete(assetPath)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}

async deleteOldEditorAsset(workspaceId: string, src: string): Promise<any> {
const assetKey = getAssetIdFromUrl(src);
return this.delete(`/api/workspaces/file-assets/${workspaceId}/${assetKey}/`)
.then((response) => response?.status)
.catch((error) => {
throw error?.response?.data;
});
}

/**
* Restores a new asset
* @param {string} workspaceSlug - The workspace slug
* @param {string} src - The asset source
* @returns {Promise<void>} Promise resolving to void
* @throws {Error} If the request fails
*/
async restoreNewAsset(workspaceSlug: string, src: string): Promise<void> {
// remove the last slash and get the asset id
const assetId = getAssetIdFromUrl(src);
Expand All @@ -84,16 +108,10 @@ export class FileService extends APIService {
});
}

async restoreOldEditorAsset(workspaceId: string, src: string): Promise<void> {
const assetKey = getAssetIdFromUrl(src);
return this.post(`/api/workspaces/file-assets/${workspaceId}/${assetKey}/restore/`)
.then((response) => response?.data)
.catch((error) => {
throw error?.response?.data;
});
}

/**
* Cancels the upload
*/
cancelUpload() {
this.cancelSource.cancel("Upload cancelled");
this.cancelSource.cancelUpload();
prateekshourya29 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading