Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/linear

src/list-workflow-states.ts

99 lines · 3.2 KB · TypeScript
import { linearGraphql } from './client.ts'
import { buildWorkflowStateFilter } from './filters.ts'
import {
	mapWorkflowState,
	nodesFromConnection,
	pageInfoFromConnection,
	PAGE_INFO_FIELDS,
	WORKFLOW_STATE_FIELDS,
} from './models.ts'
import type {
	LinearAuthInput,
	LinearPageInfo,
	LinearWorkflowStateSummary,
	WorkflowStateType,
} from './types.ts'
import {
	clampInt,
	optionalBoolean,
	optionalString,
	optionalWorkflowStateType,
	requireRecord,
} from './types.ts'
import { requireAuthMode } from './auth.ts'

export type ListWorkflowStatesInput = LinearAuthInput & {
	first?: number
	after?: string
	includeArchived?: boolean
	teamId?: string
	teamKey?: string
	type?: WorkflowStateType
}

export type ListWorkflowStatesResult = {
	items: Array<LinearWorkflowStateSummary>
	pageInfo: LinearPageInfo
}

/**
 * List Linear workflow states (issue statuses), optionally for one team.
 * @example
 * import listWorkflowStates from 'kody:@kody/linear/list-workflow-states'
 * const { items } = await listWorkflowStates({ teamKey: 'ENG', type: 'started' })
 */
export async function listWorkflowStates(
	input: ListWorkflowStatesInput = {},
): Promise<ListWorkflowStatesResult> {
	const first = clampInt(input.first, 1, 50, 50)
	const filter = buildWorkflowStateFilter({
		teamId: optionalString(input.teamId, 'teamId'),
		teamKey: optionalString(input.teamKey, 'teamKey'),
		type: optionalWorkflowStateType(input.type, 'type'),
	})
	const result = await linearGraphql<{ workflowStates: unknown }>({
		...input,
		operation: 'query',
		query: `query ListWorkflowStates($first: Int!, $after: String, $filter: WorkflowStateFilter, $includeArchived: Boolean) {
			workflowStates(first: $first, after: $after, filter: $filter, includeArchived: $includeArchived) {
				${PAGE_INFO_FIELDS}
				nodes { ${WORKFLOW_STATE_FIELDS} }
			}
		}`,
		variables: {
			first,
			after: optionalString(input.after, 'after') ?? null,
			filter: filter ?? null,
			includeArchived: optionalBoolean(input.includeArchived, 'includeArchived') ?? false,
		},
	})
	return {
		items: nodesFromConnection(result.data.workflowStates).map(mapWorkflowState),
		pageInfo: pageInfoFromConnection(result.data.workflowStates),
	}
}

/**
 * List Linear workflow states (issue statuses), optionally for one team.
 * @example
 * import listWorkflowStates from 'kody:@kody/linear/list-workflow-states'
 * const result = await listWorkflowStates({ teamKey: 'ENG' })
 */
export default async function listWorkflowStatesEntrypoint(
	params: ListWorkflowStatesInput & Record<string, unknown> = {},
) {
	const input = requireRecord(params, 'list-workflow-states')
	return listWorkflowStates({
		first: input.first as number | undefined,
		after: optionalString(input.after, 'after'),
		includeArchived: optionalBoolean(input.includeArchived, 'includeArchived'),
		teamId: optionalString(input.teamId, 'teamId'),
		teamKey: optionalString(input.teamKey, 'teamKey'),
		type: optionalWorkflowStateType(input.type, 'type'),
		integrationName: optionalString(input.integrationName, 'integrationName'),
		integration: optionalString(input.integration, 'integration'),
		account: optionalString(input.account, 'account'),
		secretName: optionalString(input.secretName, 'secretName'),
		auth: requireAuthMode(input.auth),
	})
}