-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
7 changed files
with
138 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { EntryPoint, EntryPoints, RestProjectsOptions } from '../EntryPoint'; | ||
import { logger } from '../Utils'; | ||
import { Arguments, Argv, CommandModule } from 'yargs'; | ||
import { container } from 'tsyringe'; | ||
|
||
export class GetEntryPoints implements CommandModule { | ||
public readonly command = 'get-entrypoints [options]'; | ||
public readonly describe = 'get all entrypoints of the project.'; | ||
|
||
public builder(argv: Argv): Argv { | ||
return argv | ||
.option('token', { | ||
alias: 't', | ||
describe: 'Bright API-key', | ||
requiresArg: true, | ||
demandOption: true | ||
}) | ||
.option('project', { | ||
alias: 'p', | ||
describe: 'ID of the project', | ||
requiresArg: true, | ||
demandOption: true | ||
}) | ||
.middleware((args: Arguments) => | ||
container.register<RestProjectsOptions>(RestProjectsOptions, { | ||
useValue: { | ||
insecure: args.insecure as boolean, | ||
baseURL: args.api as string, | ||
apiKey: args.token as string, | ||
proxyURL: (args.proxyExternal ?? args.proxy) as string | ||
} | ||
}) | ||
); | ||
} | ||
|
||
public async handler(args: Arguments): Promise<void> { | ||
const entryPointsManager: EntryPoints = container.resolve(EntryPoints); | ||
|
||
try { | ||
const entryPoints: EntryPoint[] = await entryPointsManager.entrypoints( | ||
args.project as string | ||
); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(entryPoints); | ||
process.exit(0); | ||
} catch (e) { | ||
logger.error(`Error during "get-entrypoints": ${e.error || e.message}`); | ||
process.exit(1); | ||
} | ||
} | ||
} |
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
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,10 @@ | ||
export interface EntryPoints { | ||
entrypoints(projectId: string): Promise<EntryPoint[]>; | ||
} | ||
|
||
export const EntryPoints: unique symbol = Symbol('EntryPoints'); | ||
|
||
export interface EntryPoint { | ||
id: string; | ||
url: string; | ||
} |
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,63 @@ | ||
import { EntryPoints, EntryPoint } from './EntryPoints'; | ||
import { ProxyFactory } from '../Utils'; | ||
import axios, { Axios } from 'axios'; | ||
import { inject, injectable } from 'tsyringe'; | ||
import http from 'node:http'; | ||
import https from 'node:https'; | ||
|
||
export interface RestProjectsOptions { | ||
baseURL: string; | ||
apiKey: string; | ||
timeout?: number; | ||
insecure?: boolean; | ||
proxyURL?: string; | ||
} | ||
|
||
export const RestProjectsOptions: unique symbol = Symbol('RestProjectsOptions'); | ||
|
||
@injectable() | ||
export class RestEntryPoints implements EntryPoints { | ||
private readonly client: Axios; | ||
|
||
constructor( | ||
@inject(ProxyFactory) private readonly proxyFactory: ProxyFactory, | ||
@inject(RestProjectsOptions) | ||
{ | ||
baseURL, | ||
apiKey, | ||
insecure, | ||
proxyURL, | ||
timeout = 10000 | ||
}: RestProjectsOptions | ||
) { | ||
const { | ||
httpAgent = new http.Agent(), | ||
httpsAgent = new https.Agent({ rejectUnauthorized: !insecure }) | ||
} = proxyURL | ||
? this.proxyFactory.createProxy({ | ||
proxyUrl: proxyURL, | ||
rejectUnauthorized: !insecure | ||
}) | ||
: {}; | ||
|
||
this.client = axios.create({ | ||
baseURL, | ||
timeout, | ||
httpAgent, | ||
httpsAgent, | ||
responseType: 'json', | ||
headers: { authorization: `Api-Key ${apiKey}` } | ||
}); | ||
} | ||
|
||
public async entrypoints(projectId: string): Promise<EntryPoint[]> { | ||
const res = await this.client.get( | ||
`/api/v2/projects/${projectId}/entry-points` | ||
); | ||
|
||
return res.data.items.map((raw: any) => ({ | ||
id: raw.id, | ||
url: raw.url | ||
})); | ||
} | ||
} |
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,2 @@ | ||
export * from './EntryPoints'; | ||
export * from './RestEntryPoints'; |
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