Skip to content

Commit

Permalink
fix: apps create command
Browse files Browse the repository at this point in the history
* Update API version to 2022-03-11~experimental
* Update formatting out app create output
* Update field name for data fetched from the API
* Upate make request rest to handle 400 and greater than 400 errors separately
* Add no compression option to request payload
  • Loading branch information
love-bhardwaj committed Nov 2, 2022
1 parent 234566f commit 8544c06
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 25 deletions.
5 changes: 3 additions & 2 deletions src/cli/commands/apps/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export async function createApp(
url: getAppsURL(EAppsURL.CREATE_APP, { orgId }),
body: {
name,
redirectUris,
redirect_uris: redirectUris,
scopes,
},
qs: {
version: '2021-08-11~experimental',
version: '2022-03-11~experimental',
},
noCompression: true,
};

try {
Expand Down
23 changes: 13 additions & 10 deletions src/lib/apps/rest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,27 @@ Meta:\t${meta}`;
}

export function handleCreateAppRes(res: ICreateAppResponse): string {
debug(res);
const {
name,
clientId,
redirectUris,
client_id,
redirect_uris,
scopes,
isPublic,
clientSecret,
is_public,
client_secret,
access_token_ttl_seconds,
} = res.data.attributes;

return `Snyk App created successfully!
Please ensure you save the following details:
App Name:\t${name}
Client ID:\t${clientId}
Redirect URIs:\t${redirectUris}
Scopes:\t${scopes}
Is App Public:\t${isPublic}
App Name: ${name}
Client ID: ${client_id}
Redirect URIs: ${redirect_uris}
Scopes: ${scopes}
Is App Public: ${is_public}
Access token TTL seconds: ${access_token_ttl_seconds}
Client Secret (${chalk.redBright(
'keep it safe and protected',
)}):\t${clientSecret}`;
)}): ${client_secret}`;
}
9 changes: 5 additions & 4 deletions src/lib/apps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export interface ICreateAppResponse {
id: string;
attributes: {
name: string;
clientId: string;
redirectUris: string[];
client_id: string;
redirect_uris: string[];
scopes: string[];
isPublic: boolean;
clientSecret: string;
is_public: boolean;
client_secret: string;
access_token_ttl_seconds: number;
};
links: {
self: string;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/request/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ export async function makeRequestRest<T>(payload: any): Promise<T> {
if (error) {
return reject(error);
}
if (res.statusCode >= 400) {
if (res.statusCode === 400) {
return reject({
code: res.statusCode,
body: JSON.parse(body as any),
});
} else if (res.statusCode >= 401) {
return reject({
code: res.statusCode,
});
}
resolve(JSON.parse(body as any) as T);
});
Expand Down
8 changes: 6 additions & 2 deletions src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function setupRequest(payload: Payload) {

const versionNumber = getVersion();
const body = payload.body;
let data;
let data = body;

delete payload.body;

Expand All @@ -40,7 +40,10 @@ function setupRequest(payload: Payload) {

payload.headers['x-snyk-cli-version'] = versionNumber;

if (body) {
const noCompression = payload.noCompression;

if (body && !noCompression) {
debug('compressing request body');
const json = JSON.stringify(body);
if (json.length < 1e4) {
debug(JSON.stringify(body, null, 2));
Expand Down Expand Up @@ -134,6 +137,7 @@ export async function makeRequest(
payload: Payload,
): Promise<{ res: needle.NeedleResponse; body: any }> {
const { method, url, data, options } = setupRequest(payload);
debug(data);

return new Promise((resolve, reject) => {
needle.request(method, url, data, options, (err, res, respBody) => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/request/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Payload {
json?: boolean;
timeout?: number;
family?: number;
noCompression?: boolean;
}
13 changes: 7 additions & 6 deletions test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
app.post(`${basePath}/orgs/:orgId/apps`, (req, res) => {
const { orgId } = req.params;
const name = req.body.name;
const redirectUris = req.body.redirectUris;
const redirect_uris = req.body.redirect_uris;
const scopes = req.body.scopes;
res.send(
JSON.stringify({
Expand All @@ -463,14 +463,15 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
id: '84144c1d-a491-4fe5-94d1-ba143ba71b6d',
attributes: {
name,
clientId: '9f26c6c6-e04b-4310-8ce4-c3a6289d0633',
redirectUris,
client_id: '9f26c6c6-e04b-4310-8ce4-c3a6289d0633',
redirect_uris,
scopes,
isPublic: false,
clientSecret: 'super-secret-client-secret',
is_public: false,
client_secret: 'super-secret-client-secret',
access_token_ttl_seconds: 3600,
},
links: {
self: `/orgs/${orgId}/apps?version=2021-08-11~experimental`,
self: `/orgs/${orgId}/apps?version=2022-03-11~experimental`,
},
},
}),
Expand Down

0 comments on commit 8544c06

Please sign in to comment.