-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(api): added workflows api route for dynamic discovery #2892
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
530a329
feat(api): added workflows api route for dynamic discovery
waleedlatif1 a2c794a
added ability to edit parameter and workflow descriptions
waleedlatif1 b46f760
added new rate limit category, ack PR comments
waleedlatif1 609a8a5
fix hasChanges logic
waleedlatif1 f551f5e
added whitespace trimming before hasChanges check
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { db } from '@sim/db' | ||
| import { permissions, workflow, workflowBlocks } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { extractInputFieldsFromBlocks } from '@/lib/workflows/input-format' | ||
| import { createApiResponse, getUserLimits } from '@/app/api/v1/logs/meta' | ||
| import { checkRateLimit, createRateLimitResponse } from '@/app/api/v1/middleware' | ||
|
|
||
| const logger = createLogger('V1WorkflowDetailsAPI') | ||
|
|
||
| export const revalidate = 0 | ||
|
|
||
| export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| const requestId = crypto.randomUUID().slice(0, 8) | ||
|
|
||
| try { | ||
| const rateLimit = await checkRateLimit(request, 'workflow-detail') | ||
| if (!rateLimit.allowed) { | ||
| return createRateLimitResponse(rateLimit) | ||
| } | ||
|
|
||
| const userId = rateLimit.userId! | ||
| const { id } = await params | ||
|
|
||
| logger.info(`[${requestId}] Fetching workflow details for ${id}`, { userId }) | ||
|
|
||
| const rows = await db | ||
| .select({ | ||
| id: workflow.id, | ||
| name: workflow.name, | ||
| description: workflow.description, | ||
| color: workflow.color, | ||
| folderId: workflow.folderId, | ||
| workspaceId: workflow.workspaceId, | ||
| isDeployed: workflow.isDeployed, | ||
| deployedAt: workflow.deployedAt, | ||
| runCount: workflow.runCount, | ||
| lastRunAt: workflow.lastRunAt, | ||
| variables: workflow.variables, | ||
| createdAt: workflow.createdAt, | ||
| updatedAt: workflow.updatedAt, | ||
| }) | ||
| .from(workflow) | ||
| .innerJoin( | ||
| permissions, | ||
| and( | ||
| eq(permissions.entityType, 'workspace'), | ||
| eq(permissions.entityId, workflow.workspaceId), | ||
| eq(permissions.userId, userId) | ||
| ) | ||
| ) | ||
| .where(eq(workflow.id, id)) | ||
| .limit(1) | ||
|
|
||
| const workflowData = rows[0] | ||
| if (!workflowData) { | ||
| return NextResponse.json({ error: 'Workflow not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| const blockRows = await db | ||
| .select({ | ||
| id: workflowBlocks.id, | ||
| type: workflowBlocks.type, | ||
| subBlocks: workflowBlocks.subBlocks, | ||
| }) | ||
| .from(workflowBlocks) | ||
| .where(eq(workflowBlocks.workflowId, id)) | ||
|
|
||
| const blocksRecord = Object.fromEntries( | ||
| blockRows.map((block) => [block.id, { type: block.type, subBlocks: block.subBlocks }]) | ||
| ) | ||
| const inputs = extractInputFieldsFromBlocks(blocksRecord) | ||
|
|
||
| const response = { | ||
| id: workflowData.id, | ||
| name: workflowData.name, | ||
| description: workflowData.description, | ||
| color: workflowData.color, | ||
| folderId: workflowData.folderId, | ||
| workspaceId: workflowData.workspaceId, | ||
| isDeployed: workflowData.isDeployed, | ||
| deployedAt: workflowData.deployedAt?.toISOString() || null, | ||
| runCount: workflowData.runCount, | ||
| lastRunAt: workflowData.lastRunAt?.toISOString() || null, | ||
| variables: workflowData.variables || {}, | ||
| inputs, | ||
| createdAt: workflowData.createdAt.toISOString(), | ||
| updatedAt: workflowData.updatedAt.toISOString(), | ||
| } | ||
|
|
||
| const limits = await getUserLimits(userId) | ||
|
|
||
| const apiResponse = createApiResponse({ data: response }, limits, rateLimit) | ||
|
|
||
| return NextResponse.json(apiResponse.body, { headers: apiResponse.headers }) | ||
| } catch (error: unknown) { | ||
| const message = error instanceof Error ? error.message : 'Unknown error' | ||
| logger.error(`[${requestId}] Workflow details fetch error`, { error: message }) | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { db } from '@sim/db' | ||
| import { permissions, workflow } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, asc, eq, gt, or } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { createApiResponse, getUserLimits } from '@/app/api/v1/logs/meta' | ||
| import { checkRateLimit, createRateLimitResponse } from '@/app/api/v1/middleware' | ||
|
|
||
| const logger = createLogger('V1WorkflowsAPI') | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
| export const revalidate = 0 | ||
|
|
||
| const QueryParamsSchema = z.object({ | ||
| workspaceId: z.string(), | ||
| folderId: z.string().optional(), | ||
| deployedOnly: z.coerce.boolean().optional().default(false), | ||
| limit: z.coerce.number().min(1).max(100).optional().default(50), | ||
| cursor: z.string().optional(), | ||
| }) | ||
|
|
||
| interface CursorData { | ||
| sortOrder: number | ||
| createdAt: string | ||
| id: string | ||
| } | ||
|
|
||
| function encodeCursor(data: CursorData): string { | ||
| return Buffer.from(JSON.stringify(data)).toString('base64') | ||
| } | ||
|
|
||
| function decodeCursor(cursor: string): CursorData | null { | ||
| try { | ||
| return JSON.parse(Buffer.from(cursor, 'base64').toString()) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const requestId = crypto.randomUUID().slice(0, 8) | ||
|
|
||
| try { | ||
| const rateLimit = await checkRateLimit(request, 'workflows') | ||
| if (!rateLimit.allowed) { | ||
| return createRateLimitResponse(rateLimit) | ||
| } | ||
|
|
||
| const userId = rateLimit.userId! | ||
| const { searchParams } = new URL(request.url) | ||
| const rawParams = Object.fromEntries(searchParams.entries()) | ||
|
|
||
| const validationResult = QueryParamsSchema.safeParse(rawParams) | ||
| if (!validationResult.success) { | ||
| return NextResponse.json( | ||
| { error: 'Invalid parameters', details: validationResult.error.errors }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const params = validationResult.data | ||
|
|
||
| logger.info(`[${requestId}] Fetching workflows for workspace ${params.workspaceId}`, { | ||
| userId, | ||
| filters: { | ||
| folderId: params.folderId, | ||
| deployedOnly: params.deployedOnly, | ||
| }, | ||
| }) | ||
|
|
||
| const conditions = [ | ||
| eq(workflow.workspaceId, params.workspaceId), | ||
| eq(permissions.entityType, 'workspace'), | ||
| eq(permissions.entityId, params.workspaceId), | ||
| eq(permissions.userId, userId), | ||
| ] | ||
|
|
||
| if (params.folderId) { | ||
| conditions.push(eq(workflow.folderId, params.folderId)) | ||
| } | ||
|
|
||
| if (params.deployedOnly) { | ||
| conditions.push(eq(workflow.isDeployed, true)) | ||
| } | ||
|
|
||
| if (params.cursor) { | ||
| const cursorData = decodeCursor(params.cursor) | ||
| if (cursorData) { | ||
| const cursorCondition = or( | ||
| gt(workflow.sortOrder, cursorData.sortOrder), | ||
| and( | ||
| eq(workflow.sortOrder, cursorData.sortOrder), | ||
| gt(workflow.createdAt, new Date(cursorData.createdAt)) | ||
| ), | ||
| and( | ||
| eq(workflow.sortOrder, cursorData.sortOrder), | ||
| eq(workflow.createdAt, new Date(cursorData.createdAt)), | ||
| gt(workflow.id, cursorData.id) | ||
| ) | ||
| ) | ||
| if (cursorCondition) { | ||
| conditions.push(cursorCondition) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const orderByClause = [asc(workflow.sortOrder), asc(workflow.createdAt), asc(workflow.id)] | ||
|
|
||
| const rows = await db | ||
| .select({ | ||
| id: workflow.id, | ||
| name: workflow.name, | ||
| description: workflow.description, | ||
| color: workflow.color, | ||
| folderId: workflow.folderId, | ||
| workspaceId: workflow.workspaceId, | ||
| isDeployed: workflow.isDeployed, | ||
| deployedAt: workflow.deployedAt, | ||
| runCount: workflow.runCount, | ||
| lastRunAt: workflow.lastRunAt, | ||
| sortOrder: workflow.sortOrder, | ||
| createdAt: workflow.createdAt, | ||
| updatedAt: workflow.updatedAt, | ||
| }) | ||
| .from(workflow) | ||
| .innerJoin( | ||
| permissions, | ||
| and( | ||
| eq(permissions.entityType, 'workspace'), | ||
| eq(permissions.entityId, params.workspaceId), | ||
| eq(permissions.userId, userId) | ||
| ) | ||
| ) | ||
| .where(and(...conditions)) | ||
| .orderBy(...orderByClause) | ||
| .limit(params.limit + 1) | ||
|
|
||
| const hasMore = rows.length > params.limit | ||
| const data = rows.slice(0, params.limit) | ||
|
|
||
| let nextCursor: string | undefined | ||
| if (hasMore && data.length > 0) { | ||
| const lastWorkflow = data[data.length - 1] | ||
| nextCursor = encodeCursor({ | ||
| sortOrder: lastWorkflow.sortOrder, | ||
| createdAt: lastWorkflow.createdAt.toISOString(), | ||
| id: lastWorkflow.id, | ||
| }) | ||
| } | ||
|
|
||
| const formattedWorkflows = data.map((w) => ({ | ||
| id: w.id, | ||
| name: w.name, | ||
| description: w.description, | ||
| color: w.color, | ||
| folderId: w.folderId, | ||
| workspaceId: w.workspaceId, | ||
| isDeployed: w.isDeployed, | ||
| deployedAt: w.deployedAt?.toISOString() || null, | ||
| runCount: w.runCount, | ||
| lastRunAt: w.lastRunAt?.toISOString() || null, | ||
| createdAt: w.createdAt.toISOString(), | ||
| updatedAt: w.updatedAt.toISOString(), | ||
| })) | ||
|
|
||
| const limits = await getUserLimits(userId) | ||
|
|
||
| const response = createApiResponse( | ||
| { | ||
| data: formattedWorkflows, | ||
| nextCursor, | ||
| }, | ||
| limits, | ||
| rateLimit | ||
| ) | ||
|
|
||
| return NextResponse.json(response.body, { headers: response.headers }) | ||
| } catch (error: unknown) { | ||
| const message = error instanceof Error ? error.message : 'Unknown error' | ||
| logger.error(`[${requestId}] Workflows fetch error`, { error: message }) | ||
| return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API inaccessible for workflows without workspaceId
Medium Severity
The workflow detail endpoint joins with permissions using
workflow.workspaceId, but workflows can legitimately have a nullworkspaceId(confirmed by the existing/api/workflowsPOST endpoint which setsworkspaceId: workspaceId || null). WhenworkspaceIdis null, the SQL join conditioneq(permissions.entityId, NULL)never matches, causing the query to return no rows. This makes workflows without a workspace permanently inaccessible through this API, even to their owner (workflow.userId).