// © 2026 Evntx FZCO, Dubai, UAE. All rights reserved. // Reference connector (#DEVKIT-REFERENCE-CONNECTORS-1): OAuth2 sign-in, cursor pagination and retry on 429/5xx. // Built only against the public connector framework package. The API is a generic task service // (https://api.example.com/v1); replace the URLs, scopes and shapes with your service's. import { createAction, createConnector, ConnectorAuth, Property } from '@aancer/connector-framework'; export const API = 'https://api.example.com/v1'; // The installation runs the OAuth2 flow and refreshes the token; a step only ever sees auth.access_token. export const auth = ConnectorAuth.OAuth2({ displayName: 'Example Tasks account', description: 'Sign in with the account whose tasks the flows may read and create.', required: true, authUrl: 'https://api.example.com/oauth/authorize', tokenUrl: 'https://api.example.com/oauth/token', scope: ['tasks.read', 'tasks.write'], pkce: true, }); export type Task = { id: string; title: string; done: boolean; updatedAt: string }; type Page = { data: Task[]; next_cursor: string | null }; const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); /** Wait before retry `attempt`: the server's Retry-After (seconds or an HTTP date), else 0.5 s doubling; capped at 30 s. */ export function retryDelay(retryAfter: string | null, attempt: number, now = Date.now()): number { let ms = 500 * 2 ** attempt; if (retryAfter !== null && retryAfter.trim() !== '') { const s = Number(retryAfter); ms = Number.isFinite(s) ? s * 1000 : Math.max(0, Date.parse(retryAfter) - now) || ms; } return Math.min(Math.max(ms, 0), 30_000); } /** One API call: JSON in and out, 30 s timeout, up to 4 retries on 429 and 5xx; a 401 says to reconnect. */ export async function call(token: string, method: 'GET' | 'POST', path: string, body?: unknown): Promise { for (let attempt = 0; ; attempt++) { const res = await fetch(`${API}${path}`, { method, headers: { authorization: `Bearer ${token}`, accept: 'application/json', ...(body === undefined ? {} : { 'content-type': 'application/json' }) }, body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(30_000), }); if ((res.status === 429 || res.status >= 500) && attempt < 4) { await sleep(retryDelay(res.headers.get('retry-after'), attempt)); continue; } if (res.status === 401) throw new Error('Example Tasks: the sign-in has expired or was revoked. Reconnect the connection.'); if (!res.ok) throw new Error(`Example Tasks ${method} ${path}: HTTP ${res.status} ${(await res.text()).slice(0, 300)}`); return (res.status === 204 ? undefined : await res.json()) as T; } } export const listTasks = createAction({ auth, name: 'list_tasks', displayName: 'List tasks', description: 'Returns tasks, following the cursor from page to page until the limit.', props: { onlyOpen: Property.Checkbox({ displayName: 'Only open tasks', required: false, defaultValue: false }), maxItems: Property.Number({ displayName: 'At most', description: '1 to 5000', required: false, defaultValue: 200 }), }, async run({ auth, propsValue }) { const max = Math.min(Math.max(Math.trunc(propsValue.maxItems ?? 200), 1), 5000); const tasks: Task[] = []; let cursor: string | null = null; do { const q = new URLSearchParams({ limit: String(Math.min(100, max - tasks.length)) }); if (propsValue.onlyOpen) q.set('done', 'false'); if (cursor) q.set('cursor', cursor); const page: Page = await call(auth.access_token, 'GET', `/tasks?${q}`); tasks.push(...page.data); cursor = page.next_cursor; } while (cursor && tasks.length < max); return { tasks: tasks.slice(0, max), complete: cursor === null }; }, }); export const createTask = createAction({ auth, name: 'create_task', displayName: 'Create task', description: 'Creates one task and returns it.', props: { title: Property.ShortText({ displayName: 'Title', required: true }), }, async run({ auth, propsValue }) { const title = propsValue.title.trim(); if (!title) throw new Error('Title is empty'); return call(auth.access_token, 'POST', '/tasks', { title }); }, }); export const exampleTasks = createConnector({ displayName: 'Reference: OAuth2 tasks', description: 'Lists and creates tasks of a REST API, signed in with OAuth2.', logoUrl: 'https://api.example.com/logo.svg', minimumSupportedRelease: '0.0.0', authors: ['aancer'], auth, actions: [listTasks, createTask], triggers: [], });