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

Make API extensible #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ Temporary Items

.rcappsconfig
.vscode

package-lock.json
yarn.lock
4 changes: 3 additions & 1 deletion FigmaApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { BlockActionHandler } from './src/handlers/BlockActionHandler';
import { ExecuteReplyHandler } from './src/handlers/reply';
import { modalTitle } from './src/enums/enums';
import { CommentModalHandler } from './src/handlers/comment';
import { meUrl } from './src/lib/const';

export class FigmaApp extends App {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
Expand Down Expand Up @@ -190,7 +191,8 @@ export class FigmaApp extends App {
persistence: IPersistence
) {
if (authData) {
const userData = await http.get('https://api.figma.com/v1/me', {
const url = meUrl()
const userData = await http.get(url, {
headers: {
Authorization: `Bearer ${authData.token}`
}
Expand Down
5,066 changes: 0 additions & 5,066 deletions package-lock.json

This file was deleted.

9 changes: 5 additions & 4 deletions src/handlers/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { botNotifyCurrentUser, sendMessage } from '../lib/messages';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { getRequest } from '../helpers/Figma.sdk';
import { getProjectFilesUrl, getTeamProjectsUrl } from '../lib/const';

type file = {
key: string;
Expand Down Expand Up @@ -63,14 +64,14 @@ export class BlockActionHandler {
success: false
};
}

const teamUrl=getTeamProjectsUrl(teamId);
switch (resource_type) {
case 'file':
await getRequest(
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${teamId}/projects`
teamUrl
)
.then(async (res: IHttpResponse) => {
if (!res) {
Expand All @@ -92,7 +93,7 @@ export class BlockActionHandler {
if (projectIds) {
const reqUrls = projectIds.map(
(projectId) =>
`https://api.figma.com/v1/projects/${projectId}/files`
getProjectFilesUrl(projectId)
);

try {
Expand Down Expand Up @@ -200,7 +201,7 @@ export class BlockActionHandler {
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${teamId}/projects`
teamUrl
)
.then(async (res) => {
if (!res) {
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { UIKitViewSubmitInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
import { FigmaApp } from '../../FigmaApp';
import { getFileCommentsUrl } from '../lib/const';
import { botNotifyCurrentUser } from '../lib/messages';
import { getAccessTokenForUser } from '../storage/users';

Expand All @@ -35,9 +36,10 @@ export class CommentModalHandler {
}
};
console.log('post data - ', postData, view.commentData);
const url = getFileCommentsUrl(view.commentData);
this.http
.post(
`https://api.figma.com/v1/files/${view.commentData}/comments`,
url,
postData
)
.then(async (res) => {
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { UIKitViewSubmitInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
import { FigmaApp } from '../../FigmaApp';
import { getFileCommentsUrl } from '../lib/const';
import { botNotifyCurrentUser } from '../lib/messages';
import { getAccessTokenForUser } from '../storage/users';

Expand Down Expand Up @@ -36,10 +37,10 @@ export class ExecuteReplyHandler {
}
};
console.log('post data - ', postData, view.block);

const url = getFileCommentsUrl(view.commentData.fileKey);
this.http
.post(
`https://api.figma.com/v1/files/${view.commentData.fileKey}/comments`,
url,
postData
)
.then(async (res) => {
Expand Down
9 changes: 6 additions & 3 deletions src/helpers/getResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
IRead
} from '@rocket.chat/apps-engine/definition/accessors';
import { UIKitViewSubmitInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
import { getProjectFilesUrl, getTeamProjectsUrl } from '../lib/const';
import { getRequest } from './Figma.sdk';

export class GetResourcesFromFigma {
Expand All @@ -13,15 +14,16 @@ export class GetResourcesFromFigma {
context: UIKitViewSubmitInteractionContext,
teamId: string
) {
const url = getTeamProjectsUrl(teamId);
await getRequest(
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${teamId}/projects`
url
).then(async (team_response) => {
const reqUrls = team_response.data.projects.map(
(project: any) =>
`https://api.figma.com/v1/projects/${project.id}/files`
getProjectFilesUrl(project.id)
);
return await Promise.all(
reqUrls.map(
Expand All @@ -35,11 +37,12 @@ export class GetResourcesFromFigma {
context: UIKitViewSubmitInteractionContext,
teamId: string
) {
const url2=getTeamProjectsUrl(teamId);
await getRequest(
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${teamId}/projects`
url2
).then((res) => {
console.log('response from figma for projects - ', res.data);
});
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/teamFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IRead
} from '@rocket.chat/apps-engine/definition/accessors';
import { UIKitViewSubmitInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
import { getProjectFilesUrl, getTeamProjectsUrl } from '../lib/const';
import { getRequest } from './Figma.sdk';

// create a function to get all the files inside a project from figma using this code
Expand All @@ -15,15 +16,16 @@ export async function getAllTeamFiles(
http: IHttp,
teamId: string
) {
const teamUrl=getTeamProjectsUrl(teamId);
await getRequest(
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${teamId}/projects`
teamUrl
).then(async (team_response) => {
const reqUrls = team_response.data.projects.map(
(project: any) =>
`https://api.figma.com/v1/projects/${project.id}/files`
getProjectFilesUrl(project.id)
);
try {
await Promise.all(
Expand Down
41 changes: 41 additions & 0 deletions src/lib/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const ApiBaseUrl: string = 'https://api.figma.com';

const ApiVersion = {
V1: 'v1',
V2: 'v2'
};

const ApiEndpoint = {
Team:'teams',
Project:'projects',
File:'files',
Webhook:'webhooks',
Comment:'comments',
Me:'me'
};

export const getTeamProjectsUrl =(teamId:string)=>{
return `${ApiBaseUrl}/${ApiVersion.V1}/${ApiEndpoint.Team}/${teamId}/${ApiEndpoint.Project}`;
};

export const getProjectFilesUrl =(projectId:string)=>{
return `${ApiBaseUrl}/${ApiVersion.V1}/${ApiEndpoint.Project}/${projectId}/${ApiEndpoint.File}`;
};

export const getFilesUrl =(fileId:string)=>{
return `${ApiBaseUrl}/${ApiVersion.V1}/${ApiEndpoint.File}/${fileId}`;
};

export const getFileCommentsUrl =(fileId:string)=>{
return `${ApiBaseUrl}/${ApiVersion.V1}/${ApiEndpoint.File}/${fileId}/${ApiEndpoint.Comment}`;
};

export const webHooksUrl =()=>{
return `${ApiBaseUrl}/${ApiVersion.V2}/${ApiEndpoint.Webhook}`;
};

export const meUrl =()=>{
return `${ApiBaseUrl}/${ApiVersion.V2}/${ApiEndpoint.Me}`;
};


12 changes: 7 additions & 5 deletions src/lib/createSubscriptionMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
RocketChatAssociationRecord,
} from "@rocket.chat/apps-engine/definition/metadata";
import { IModalContext } from "../definition";
import { getFilesUrl, getProjectFilesUrl, getTeamProjectsUrl } from "./const";

export async function createSubscription(
context: UIKitViewSubmitInteractionContext,
Expand Down Expand Up @@ -71,9 +72,9 @@ export async function createSubscription(
}
return;
}

const fileUrl=getFilesUrl(fileId);
const response = await http.get(
`https://api.figma.com/v1/files/${fileId}`,
fileUrl,
{ headers }
);

Expand Down Expand Up @@ -119,8 +120,9 @@ export async function createSubscription(
}
return;
}
const teamUrl=getTeamProjectsUrl(teamId);
const response = await http.get(
`https://api.figma.com/v1/teams/${teamId}/projects`,
teamUrl,
{ headers }
);

Expand Down Expand Up @@ -161,9 +163,9 @@ export async function createSubscription(
}
return;
}

const projecturl=getProjectFilesUrl(projectID);
const response = await http.get(
`https://api.figma.com/v1/projects/${projectID}/files`,
projecturl,
{ headers }
);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { storedRoomData } from '../definition';
import { botMessageChannel, botNotifyCurrentUser } from './messages';
import { TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
import { blockAction } from '../enums/enums';
import { getFilesUrl } from './const';

export async function getFiles(
modify: IModify,
Expand Down Expand Up @@ -78,7 +79,7 @@ export async function getFiles(
);
}
const filesDataReqUrls = removeDuplicates(room_files_ids).map(
(file_id) => `https://api.figma.com/v1/files/${file_id}`
(file_id) => getFilesUrl(file_id)
);
try {
await Promise.all(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/getProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './messages';
import { TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
import { blockAction } from '../enums/enums';
import { getProjectFilesUrl } from './const';

export async function getProjects(
modify: IModify,
Expand Down Expand Up @@ -86,7 +87,7 @@ export async function getProjects(
room_projects_ids
).map(
(projectId) =>
`https://api.figma.com/v1/projects/${projectId}/files`
getProjectFilesUrl(projectId)
);
try {
await Promise.all(
Expand Down
5 changes: 4 additions & 1 deletion src/lib/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IHttp } from "@rocket.chat/apps-engine/definition/accessors";
import { IUser } from "@rocket.chat/apps-engine/definition/users";
import { getWebhookUrl } from "../sdk/subscription.sdk";
import { webHooksUrl } from "./const";
const crypto = require("crypto");

const BaseFileHost = "https://www.figma.com/file/";
Expand All @@ -14,7 +16,8 @@ export class FigmaSDK {
constructor(private readonly http: IHttp, private readonly accessToken) {}

public createWebhook(fileID: string, webhookUrl: string) {
return this.post("https://api.figma.com/v2/webhooks", {
const webhookurl=webHooksUrl()
return this.post(webhookurl, {
active: true,
event_type: "FILE_COMMENT",
team_id: "1051788064684166795",
Expand Down
9 changes: 6 additions & 3 deletions src/subscription/addSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { events } from '../enums/enums';
import { WebhookSubscription } from '../handlers/subscription/createSubscriptionHandler';
import { updateSubscriptionHandler } from '../handlers/subscription/updateSubscriptionHandler';
import { getRequest, postRequest } from '../helpers/Figma.sdk';
import { getProjectFilesUrl, getTeamProjectsUrl, webHooksUrl } from '../lib/const';

export class AddSubscription {
constructor(
Expand Down Expand Up @@ -109,11 +110,12 @@ export class AddSubscription {
const files_in_team: string[] = [];
const projects_in_team: string[] = [];
if (!project_Ids?.length && !file_Ids?.length) {
const teamUrl=getTeamProjectsUrl(team_id);
await getRequest(
this.read,
context,
this.http,
`https://api.figma.com/v1/teams/${team_id}/projects`
teamUrl
)
.then(async (team_response) => {
//console.log('response from figma for projects - ', team_response);
Expand All @@ -124,7 +126,7 @@ export class AddSubscription {
projects_in_team.push(
project.id
);
return `https://api.figma.com/v1/projects/${project.id}/files`;
return getProjectFilesUrl(project.id);
}
);
if (projects_in_team) {
Expand Down Expand Up @@ -308,11 +310,12 @@ export class AddSubscription {
description: room.id
};
// we send request to figma webhook to create a hook for every event ( runs 5 times )
const webhookurl=webHooksUrl();
await postRequest(
this.read,
context,
this.http,
'https://api.figma.com/v2/webhooks',
webhookurl,
data
)
.then(async (response) => {
Expand Down
4 changes: 3 additions & 1 deletion src/subscription/project/newProjectSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { file } from '../../definition';
import { getRequest } from '../../helpers/Figma.sdk';
import { getProjectFilesUrl } from '../../lib/const';
import { botMessageChannel } from '../../lib/messages';
import { Subscription } from '../../sdk/webhooks.sdk';
import { getAccessTokenForUser } from '../../storage/users';
Expand Down Expand Up @@ -45,11 +46,12 @@ export async function newProjectSubscription(
await Promise.all(
project_Ids.map(async (project_id) => {
// this will run for all project ids for all these store them in one file
const projecturl=getProjectFilesUrl(project_id);
await getRequest(
read,
context,
http,
`https://api.figma.com/v1/projects/${project_id}/files`
projecturl
)
.then(async (response) => {
// 3 - got the files from figma
Expand Down
Loading