Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
code/logs

.airsync/
code/coverage/
2 changes: 1 addition & 1 deletion code/eslint.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineConfig } from "eslint/config";
import prettierConfig from "eslint-config-prettier";

export default defineConfig([
{ ignores: ["dist/**", "test/**"] },
{ ignores: ["dist/**", "coverage/**", "test/**"] },
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.browser } },
...tseslint.configs.recommended,
prettierConfig,
Expand Down
571 changes: 309 additions & 262 deletions code/package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@
"yargs": "^17.6.2"
},
"dependencies": {
"@devrev/airsync-sdk": "2.0.0",
"@devrev/typescript-sdk": "1.1.78",
"@devrev/airsync-sdk": "2.1.0",
"@devrev/typescript-sdk": "1.1.79",
"axios": "^1.20.0",
"dotenv": "^16.0.3",
"jiti": "2.5.1",
"jiti": "2.7.0",
"js-jsonl": "^1.1.1",
"yargs": "^17.6.2"
},
"engines": {
"node": ">=20"
},
"overrides": {
"nodemon": {
"minimatch": "11.0.1"
}
}
}
8 changes: 6 additions & 2 deletions code/src/functions/external-system/data-denormalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { ExternalAttachment, ExternalTodo } from './types';

export function denormalizeTodo(item: ExternalSystemItem): ExternalTodo {
return {
id: item.id.devrev,
body: item.data.body,
id: item.id.external ?? item.id.devrev,
body: typeof item.data.body === 'string'
? item.data.body
: (item.data.body?.content ?? []).map((part: string | { fallback_record_name?: string; id: string }) =>
typeof part === 'string' ? part : part.fallback_record_name ?? part.id
).join(''),
creator: item.data.creator,
owner: item.data.owner,
title: item.data.title,
Expand Down
3 changes: 2 additions & 1 deletion code/src/functions/external-system/data-normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function normalizeTodo(item: ExternalTodo): NormalizedItem {
created_date: item.created_date,
modified_date: item.modified_date,
data: {
body: item.body,
body: [item.body],
creator: item.creator,
owner: item.owner,
title: item.title,
Expand Down Expand Up @@ -53,5 +53,6 @@ export function normalizeAttachment(item: ExternalAttachment): NormalizedAttachm
file_name: item.file_name,
author_id: item.author_id,
parent_id: item.parent_id,
inline: false,
};
}
34 changes: 17 additions & 17 deletions code/src/functions/external-system/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ const TODO_LISTS: ExternalTodoList[] = [
];

const TODOS: ExternalTodo[] = [
{ id: 'todo-1', created_date: '2018-06-01T08:00:00Z', modified_date: '2018-06-01T08:00:00Z', title: 'Set up development environment', body: '<p>Install Node.js, configure editor, clone repos.</p>', creator: 'user-1', owner: 'user-1' },
{ id: 'todo-2', created_date: '2019-09-15T10:00:00Z', modified_date: '2019-09-15T10:00:00Z', title: 'Write project proposal', body: '<p>Draft the initial project proposal document.</p>', creator: 'user-2', owner: 'user-2' },
{ id: 'todo-3', created_date: '2020-02-20T09:00:00Z', modified_date: '2020-02-20T09:00:00Z', title: 'Design database schema', body: '<p>Define entities, relationships and indexes.</p>', creator: 'user-1', owner: 'user-3' },
{ id: 'todo-4', created_date: '2021-05-10T14:00:00Z', modified_date: '2021-05-10T14:00:00Z', title: 'Implement authentication', body: '<p>Add JWT-based authentication to the API.</p>', creator: 'user-3', owner: 'user-4' },
{ id: 'todo-5', created_date: '2022-01-18T11:30:00Z', modified_date: '2022-03-05T16:00:00Z', title: 'Add unit tests', body: '<p>Achieve 80% test coverage for the core module.</p>', creator: 'user-2', owner: 'user-5' },
{ id: 'todo-6', created_date: '2022-08-22T08:00:00Z', modified_date: '2023-02-14T09:00:00Z', title: 'Performance profiling', body: '<p>Profile API endpoints and fix bottlenecks.</p>', creator: 'user-4', owner: 'user-4' },
{ id: 'todo-7', created_date: '2023-04-05T10:00:00Z', modified_date: '2023-07-01T13:00:00Z', title: 'Integrate third-party payment provider', body: '<p>Connect Stripe for billing.</p>', creator: 'user-5', owner: 'user-6' },
{ id: 'todo-8', created_date: '2023-11-01T08:30:00Z', modified_date: '2024-02-10T10:00:00Z', title: 'Migrate to TypeScript', body: '<p>Convert remaining JS files to TypeScript.</p>', creator: 'user-6', owner: 'user-7' },
{ id: 'todo-9', created_date: '2024-03-12T09:00:00Z', modified_date: '2024-06-20T15:00:00Z', title: 'Implement dark mode', body: '<p>Add dark mode toggle and persist preference.</p>', creator: 'user-7', owner: 'user-8' },
{ id: 'todo-10', created_date: '2024-07-01T11:00:00Z', modified_date: '2024-09-30T12:00:00Z', title: 'Set up CI/CD pipeline', body: '<p>Configure GitHub Actions for build and deploy.</p>', creator: 'user-8', owner: 'user-9' },
{ id: 'todo-11', created_date: '2024-10-15T08:00:00Z', modified_date: '2025-01-08T09:30:00Z', title: 'Refactor API error handling', body: '<p>Standardise error responses across all endpoints.</p>', creator: 'user-9', owner: 'user-10' },
{ id: 'todo-12', created_date: '2025-02-01T10:00:00Z', modified_date: '2025-03-15T11:00:00Z', title: 'Add search functionality', body: '<p>Implement full-text search with filters.</p>', creator: 'user-10', owner: 'user-1' },
{ id: 'todo-13', created_date: '2025-05-20T09:00:00Z', modified_date: '2025-07-10T14:00:00Z', title: 'Optimise database queries', body: '<p>Add missing indexes and rewrite slow queries.</p>', creator: 'user-1', owner: 'user-2' },
{ id: 'todo-14', created_date: '2025-09-01T08:00:00Z', modified_date: '2025-11-20T16:30:00Z', title: 'Write API documentation', body: '<p>Document all public endpoints using OpenAPI spec.</p>', creator: 'user-2', owner: 'user-3' },
{ id: 'todo-15', created_date: '2026-01-10T09:00:00Z', modified_date: '2026-03-01T10:00:00Z', title: 'Launch v2.0', body: '<p>Final QA pass and production deployment.</p>', creator: 'user-3', owner: 'user-4' },
{ id: 'todo-1', created_date: '2018-06-01T08:00:00Z', modified_date: '2018-06-01T08:00:00Z', title: 'Set up development environment', body: 'Install Node.js, configure editor, clone repos.', creator: 'user-1', owner: 'user-1' },
{ id: 'todo-2', created_date: '2019-09-15T10:00:00Z', modified_date: '2019-09-15T10:00:00Z', title: 'Write project proposal', body: 'Draft the initial project proposal document.', creator: 'user-2', owner: 'user-2' },
{ id: 'todo-3', created_date: '2020-02-20T09:00:00Z', modified_date: '2020-02-20T09:00:00Z', title: 'Design database schema', body: 'Define entities, relationships and indexes.', creator: 'user-1', owner: 'user-3' },
{ id: 'todo-4', created_date: '2021-05-10T14:00:00Z', modified_date: '2021-05-10T14:00:00Z', title: 'Implement authentication', body: 'Add JWT-based authentication to the API.', creator: 'user-3', owner: 'user-4' },
{ id: 'todo-5', created_date: '2022-01-18T11:30:00Z', modified_date: '2022-03-05T16:00:00Z', title: 'Add unit tests', body: 'Achieve 80% test coverage for the core module.', creator: 'user-2', owner: 'user-5' },
{ id: 'todo-6', created_date: '2022-08-22T08:00:00Z', modified_date: '2023-02-14T09:00:00Z', title: 'Performance profiling', body: 'Profile API endpoints and fix bottlenecks.', creator: 'user-4', owner: 'user-4' },
{ id: 'todo-7', created_date: '2023-04-05T10:00:00Z', modified_date: '2023-07-01T13:00:00Z', title: 'Integrate third-party payment provider', body: 'Connect Stripe for billing.', creator: 'user-5', owner: 'user-6' },
{ id: 'todo-8', created_date: '2023-11-01T08:30:00Z', modified_date: '2024-02-10T10:00:00Z', title: 'Migrate to TypeScript', body: 'Convert remaining JS files to TypeScript.', creator: 'user-6', owner: 'user-7' },
{ id: 'todo-9', created_date: '2024-03-12T09:00:00Z', modified_date: '2024-06-20T15:00:00Z', title: 'Implement dark mode', body: 'Add dark mode toggle and persist preference.', creator: 'user-7', owner: 'user-8' },
{ id: 'todo-10', created_date: '2024-07-01T11:00:00Z', modified_date: '2024-09-30T12:00:00Z', title: 'Set up CI/CD pipeline', body: 'Configure GitHub Actions for build and deploy.', creator: 'user-8', owner: 'user-9' },
{ id: 'todo-11', created_date: '2024-10-15T08:00:00Z', modified_date: '2025-01-08T09:30:00Z', title: 'Refactor API error handling', body: 'Standardise error responses across all endpoints.', creator: 'user-9', owner: 'user-10' },
{ id: 'todo-12', created_date: '2025-02-01T10:00:00Z', modified_date: '2025-03-15T11:00:00Z', title: 'Add search functionality', body: 'Implement full-text search with filters.', creator: 'user-10', owner: 'user-1' },
{ id: 'todo-13', created_date: '2025-05-20T09:00:00Z', modified_date: '2025-07-10T14:00:00Z', title: 'Optimise database queries', body: 'Add missing indexes and rewrite slow queries.', creator: 'user-1', owner: 'user-2' },
{ id: 'todo-14', created_date: '2025-09-01T08:00:00Z', modified_date: '2025-11-20T16:30:00Z', title: 'Write API documentation', body: 'Document all public endpoints using OpenAPI spec.', creator: 'user-2', owner: 'user-3' },
{ id: 'todo-15', created_date: '2026-01-10T09:00:00Z', modified_date: '2026-03-01T10:00:00Z', title: 'Launch v2.0', body: 'Final QA pass and production deployment.', creator: 'user-3', owner: 'user-4' },
];

const ATTACHMENTS: ExternalAttachment[] = [
{ id: 'att-1', url: 'https://www.devrev.ai/favicon.ico', file_name: 'devrev-icon.ico', author_id: 'user-1', parent_id: 'todo-1' },
{ id: 'att-2', url: 'https://www.devrev.ai/favicon.ico', file_name: 'devrev-icon.ico', author_id: 'user-2', parent_id: 'todo-5' },
{ id: 'att-1', url: 'https://raw.githubusercontent.com/devrev/airsync-template/03ab27d406c3afd3286ae8f06a9de48ab9d96664/README.md', file_name: 'airsync-template-readme.md', author_id: 'user-1', parent_id: 'todo-1' },
{ id: 'att-2', url: 'https://raw.githubusercontent.com/devrev/airsync-template/03ab27d406c3afd3286ae8f06a9de48ab9d96664/README.md', file_name: 'airsync-template-readme.md', author_id: 'user-2', parent_id: 'todo-5' },
];

// ---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"primary_external_field": "body",
"reverse": true,
"transformation_method_for_set": {
"transformation_method": "use_directly"
"transformation_method": "use_rich_text"
}
},
"created_by_id": {
Expand Down
16 changes: 14 additions & 2 deletions code/src/functions/extraction/workers/data-extraction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { processExtractionTask } from '@devrev/airsync-sdk';
import { processExtractionTask, SyncMapperRecordTargetType } from '@devrev/airsync-sdk';

import { normalizeAttachment, normalizeTodo, normalizeUser } from '../../external-system/data-normalization';
import { HttpClient } from '../../external-system/http-client';
Expand Down Expand Up @@ -69,7 +69,19 @@ processExtractionTask<ExtractorState>({
continue;
}

const items = await itemTypeToExtract.extractFunction(httpClient);
let items = await itemTypeToExtract.extractFunction(httpClient);
if (itemTypeToExtract.name === 'attachments') {
const unmapped = [];
for (const item of items as ExternalAttachment[]) {
const mapped = await adapter.mappers.getByExternalId({
sync_unit: adapter.event.payload.event_context.sync_unit,
external_id: item.id,
target_type: SyncMapperRecordTargetType.ARTIFACT,
});
if (!mapped?.sync_mapper_record) unmapped.push(item);
}
items = unmapped;
}
await adapter.getRepo(itemTypeToExtract.name)?.push(items);
adapter.state[itemTypeToExtract.name].completed = true;
}
Expand Down
75 changes: 75 additions & 0 deletions code/test/data-normalization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, expect, it } from '@jest/globals';
import { createMockEvent, ExternalSystemItem } from '@devrev/airsync-sdk';

import { denormalizeTodo } from '../src/functions/external-system/data-denormalization';
import { normalizeAttachment, normalizeTodo } from '../src/functions/external-system/data-normalization';
import { HttpClient } from '../src/functions/external-system/http-client';
import mapping from '../src/functions/external-system/initial_domain_mapping.json';
import { ExternalTodo } from '../src/functions/external-system/types';

const todo: ExternalTodo = {
id: 'todo-1',
created_date: '2026-01-01T00:00:00Z',
modified_date: '2026-01-01T00:00:00Z',
title: 'Test todo',
body: 'Hello **world** & [friends](https://example.com).',
creator: 'user-1',
owner: 'user-1',
};

describe('Todo rich text', () => {
it('wraps Markdown in the rich-text array expected by AirSync', () => {
expect(normalizeTodo(todo).data).toMatchObject({
body: ['Hello **world** & [friends](https://example.com).'],
});
expect(mapping.additional_mappings.record_type_mappings.todos
.possible_record_type_mappings[0].shard.stock_field_mappings.body
.transformation_method_for_set.transformation_method).toBe('use_rich_text');
});

it('preserves paragraphs and lists', () => {
expect(normalizeTodo({ ...todo, body: 'First\n\nSecond\n\n- One\n- Two' }).data)
.toMatchObject({ body: ['First\n\nSecond\n\n- One\n- Two'] });
});

it('accepts an empty body', () => {
expect(normalizeTodo({ ...todo, body: '' }).data).toMatchObject({ body: [''] });
});

it('loads reverse-sync rich text as Markdown', () => {
const item = {
...todo,
id: { devrev: 'work-1' },
data: { ...todo, body: { type: 'rich_text', content: ['Hello **world**.'] } },
} as ExternalSystemItem;
expect(denormalizeTodo(item).body).toBe('Hello **world**.');
});

it('uses reference names when loading mixed rich-text content', () => {
const item = {
...todo,
id: { devrev: 'work-1' },
data: { ...todo, body: { type: 'rich_text', content: ['Hello ', { id: 'user-1', fallback_record_name: 'Alice' }, ' and ', { id: 'user-2' }] } },
} as ExternalSystemItem;
expect(denormalizeTodo(item).body).toBe('Hello Alice and user-2');
});

it('preserves legacy string bodies when loading', () => {
const item = { ...todo, id: { devrev: 'work-1' }, data: todo } as ExternalSystemItem;
expect(denormalizeTodo(item).body).toBe(todo.body);
});
});

describe('Todo attachment fixtures', () => {
it('produces downloadable, non-inline attachments linked to their todos', async () => {
const client = new HttpClient(createMockEvent('http://localhost:3001'));
const attachments = (await client.getAttachments()).map(normalizeAttachment);
expect(attachments).toHaveLength(2);
expect(attachments.map((item) => item.parent_id)).toEqual(['todo-1', 'todo-5']);
for (const attachment of attachments) {
expect(attachment.inline).toBe(false);
expect(attachment.url).toMatch(/^https:\/\/raw\.githubusercontent\.com\/devrev\/airsync-template\/[a-f0-9]{40}\/README\.md$/);
expect(attachment.file_name).toBe('airsync-template-readme.md');
}
});
});
Loading
Loading