Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(datasource/custom): use class based fetcher to simplify code (…
- Loading branch information
Showing
7 changed files
with
68 additions
and
60 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,14 @@ | ||
import type { CustomDatasourceFormats } from '../../../../config/types'; | ||
import { JSONFetcher } from './json'; | ||
import { PlainFetcher } from './plain'; | ||
import type { CustomDatasourceFetcher } from './types'; | ||
import { YamlFetcher } from './yaml'; | ||
|
||
export const fetchers: Record< | ||
CustomDatasourceFormats, | ||
CustomDatasourceFetcher | ||
> = { | ||
json: new JSONFetcher(), | ||
plain: new PlainFetcher(), | ||
yaml: new YamlFetcher(), | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { readLocalFile } from '../../../../util/fs'; | ||
import type { Http } from '../../../../util/http'; | ||
import type { CustomDatasourceFetcher } from './types'; | ||
|
||
export async function fetch(http: Http, url: string): Promise<unknown> { | ||
const response = await http.getJson(url); | ||
return response.body; | ||
} | ||
export class JSONFetcher implements CustomDatasourceFetcher { | ||
async fetch(http: Http, registryURL: string): Promise<unknown> { | ||
const response = await http.getJson(registryURL); | ||
return response.body; | ||
} | ||
|
||
export async function read(path: string): Promise<unknown> { | ||
const fileContent = await readLocalFile(path, 'utf8'); | ||
async readFile(registryURL: string): Promise<unknown> { | ||
const fileContent = await readLocalFile(registryURL, 'utf8'); | ||
|
||
return JSON.parse(fileContent!); | ||
return JSON.parse(fileContent!); | ||
} | ||
} |
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,6 @@ | ||
import type { Http } from '../../../../util/http'; | ||
|
||
export interface CustomDatasourceFetcher { | ||
fetch(http: Http, registryURL: string): Promise<unknown>; | ||
readFile(registryURL: string): Promise<unknown>; | ||
} |
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import yaml from 'js-yaml'; | ||
import { readLocalFile } from '../../../../util/fs'; | ||
import type { Http } from '../../../../util/http'; | ||
import type { CustomDatasourceFetcher } from './types'; | ||
|
||
export async function fetch(http: Http, url: string): Promise<unknown> { | ||
const response = await http.get(url); | ||
export class YamlFetcher implements CustomDatasourceFetcher { | ||
async fetch(http: Http, registryURL: string): Promise<unknown> { | ||
const response = await http.get(registryURL); | ||
|
||
return yaml.load(response.body); | ||
} | ||
return yaml.load(response.body); | ||
} | ||
|
||
export async function read(path: string): Promise<unknown> { | ||
const fileContent = await readLocalFile(path, 'utf8'); | ||
async readFile(registryURL: string): Promise<unknown> { | ||
const fileContent = await readLocalFile(registryURL, 'utf8'); | ||
|
||
return yaml.load(fileContent!); | ||
return yaml.load(fileContent!); | ||
} | ||
} |
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