Skip to content

Commit

Permalink
feat: providers package
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed Jan 15, 2025
1 parent 6087cdf commit 3319fdb
Show file tree
Hide file tree
Showing 6 changed files with 8,010 additions and 4 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/cli-verification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ jobs:
fi
}
check_and_publish packages/node-client
check_and_publish packages/shared
check_and_publish packages/frontend
check_and_publish packages/types
check_and_publish packages/nango-yaml
check_and_publish packages/providers
check_and_publish packages/node-client
check_and_publish packages/frontend
- id: publish_step
name: Publish npm packages to the github registry
env:
Expand All @@ -66,6 +67,7 @@ jobs:
GIT_HASH=$(git rev-parse HEAD)
echo "hash=${GIT_HASH}" >> "$GITHUB_OUTPUT"
bash ./scripts/publish.sh 0.0.1-$GIT_HASH
- name: Publish the cli privately under the correct scope
working-directory: packages/cli
env:
Expand All @@ -85,7 +87,7 @@ jobs:
steps:
- uses: actions/setup-node@v4
with:
node-version: '20.12.2'
node-version: '20.18.1'
registry-url: 'https://npm.pkg.github.com'
scope: '@nangohq'
always-auth: true
Expand Down
58 changes: 58 additions & 0 deletions packages/providers/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import fs from 'node:fs';
import yaml from 'js-yaml';
import type { Provider, ProviderAlias } from '@nangohq/types';

const __filename = fileURLToPath(import.meta.url);
const pkgRoot = path.join(__filename, '../../');

let providers: Record<string, Provider> | undefined = undefined;

export function updateProviderCache(update: Record<string, Provider>) {
providers = update;
}

export function getProviders() {
if (!providers) {
providers = loadProvidersYaml();
}

return providers;
}

export function getProvider(providerName: string): Provider | null {
const providers = getProviders();
return providers?.[providerName] ?? null;
}

function loadProvidersYaml(): Record<string, Provider> | undefined {
try {
const providersYamlPath = path.join(pkgRoot, 'providers.yaml');
const fileEntries = yaml.load(fs.readFileSync(providersYamlPath).toString()) as Record<string, Provider | ProviderAlias>;

if (fileEntries == null) {
throw new Error('provider_template_loading_failed');
}

for (const key in fileEntries) {
const entry = fileEntries[key];

if (entry && 'alias' in entry) {
if (Object.keys(entry).length <= 0) {
console.error('Failed to find alias', entry.alias);
continue;
}

const { alias, ...overrides } = entry;
const aliasData = fileEntries[entry.alias] as Provider;
fileEntries[key] = { ...aliasData, ...overrides };
}
}

return fileEntries as Record<string, Provider>;
} catch (err) {
console.error('Failed to load providers.yaml', err);
}
return;
}
21 changes: 21 additions & 0 deletions packages/providers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@nangohq/providers",
"version": "0.48.3",
"description": "Nango's shared components.",
"type": "module",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"private": false,
"scripts": {},
"dependencies": {
"js-yaml": "4.1.0"
},
"devDependencies": {
"@nangohq/types": "0.48.3",
"vitest": "2.1.8"
},
"files": [
"dist/**/*",
"providers.yaml"
]
}
Loading

0 comments on commit 3319fdb

Please sign in to comment.