-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6087cdf
commit 3319fdb
Showing
6 changed files
with
8,010 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.