Skip to content

Commit

Permalink
fix(flows): path + strictness (NangoHQ#1894)
Browse files Browse the repository at this point in the history
## Describe your changes

One last fix hopefully about Shared path. Flows' path was wrong and the
error was swallowed. tbh I wish I could just throw but I don't know the
full impact of that.

- Fix path
- Add tests
- Add more check at runtime to avoid any more surprises
  • Loading branch information
bodinsamuel authored Mar 22, 2024
1 parent 8fb6332 commit a2389d0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/shared/lib/services/flow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { loadStandardConfig } from './nango-config.service.js';
import remoteFileService from './file/remote.service.js';
import type { NangoConfig, NangoIntegration, NangoSyncConfig, NangoModelV1, StandardNangoConfig } from '../models/NangoConfig.js';
import type { HTTP_VERB } from '../models/Generic.js';
import { errorManager } from '../index.js';

export interface Config {
integrations: NangoIntegration & NangoModelV1;
Expand All @@ -15,11 +16,16 @@ export interface Config {
class FlowService {
public getAllAvailableFlows(): Config {
try {
const flowPath = path.join(dirname(), '../../../flows.yaml');
const flows = yaml.load(fs.readFileSync(flowPath).toString());
const flowPath = path.join(dirname(import.meta.url), '../../flows.yaml');
const flows = yaml.load(fs.readFileSync(flowPath).toString()) as Config;

return flows as Config;
} catch (_e) {
if (flows === undefined || !('integrations' in flows) || Object.keys(flows.integrations).length <= 0) {
throw new Error('empty_flows');
}

return flows;
} catch (err) {
errorManager.report(`failed_to_find_flows, ${JSON.stringify(err)}`);
return {} as Config;
}
}
Expand Down
36 changes: 35 additions & 1 deletion packages/shared/lib/services/flow.service.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, describe, it, vi } from 'vitest';
import { expect, describe, it, vi, afterEach } from 'vitest';
import type { Config } from './flow.service';
import FlowService from './flow.service';

Expand Down Expand Up @@ -138,6 +138,10 @@ const flows = {
};

describe('Flow service tests', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('Fetch a flow config by providing a name', () => {
vi.spyOn(FlowService, 'getAllAvailableFlows').mockImplementation(() => {
return flows as unknown as Config;
Expand All @@ -147,4 +151,34 @@ describe('Flow service tests', () => {
expect(flow).not.toBeNull();
expect(flow?.models).not.toBeUndefined();
});

it('should get flows.yaml', () => {
const flows = FlowService.getAllAvailableFlows();
expect(flows).not.toStrictEqual({});
expect(flows).toHaveProperty('integrations');
expect(Object.keys(flows.integrations).length).toBeGreaterThan(20);
expect(flows.integrations['asana']).toStrictEqual({
models: {
AsanaTask: {
completed: 'boolean',
created_at: 'date',
id: 'string',
modified_at: 'date',
name: 'string',
project_id: 'string'
}
},
syncs: {
'asana-tasks': {
description: `Fetches a list of tasks from Asana, retrieving tasks from only the first project of the first workspace
`,
endpoint: '/asana/tasks',
output: 'AsanaTask',
runs: 'every 30min',
scopes: ['default'],
sync_type: 'full'
}
}
});
});
});

0 comments on commit a2389d0

Please sign in to comment.