Skip to content

Commit

Permalink
feat: create versions enum and bump apps to GA
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceDeslo committed Nov 8, 2023
1 parent ee96e36 commit 9cccba6
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/lib/controllers/projects/projectsHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { callSnykApi } from '../../utils/api';
import { EncryptDecrypt } from '../../utils/encrypt-decrypt';
import { getMostRecentInstall } from '../../utils/authData/getMostRecent';
import { APIVersion, Envars } from '../../types';
import { Envars } from '../../types';

/**
* Get projects handler that fetches all user projects
Expand All @@ -23,7 +23,7 @@ export async function getProjectsFromApi(): Promise<unknown[]> {

// Call the axios instance configured for Snyk API v1
const requests = (data?.orgs ?? []).map((org) =>
callSnykApi(token_type, access_token, APIVersion.V1)
callSnykApi(token_type, access_token)
.post(`/org/${org.id}/projects`)
.then((project) => ({
org: org.name,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/controllers/projectsRest/projectsRestHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function getProjectsFromRestApi(): Promise<ProjectsResponse[]> {

// Call the axios instance configured for Snyk API REST
const requests = (data?.orgs ?? []).map((org) =>
callSnykRestApi(token_type, access_token, APIVersion.REST)
.get(`/orgs/${org.id}/projects?version=2023-05-29&limit=100`)
callSnykRestApi(token_type, access_token)
.get(`/orgs/${org.id}/projects?version=${APIVersion.V20230529}&limit=100`)
.then((response) => {
const projectData: ProjectData[] = response.data.data.map((project: Project) => ({
id: project.id,
Expand Down
12 changes: 11 additions & 1 deletion src/lib/types/apiVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
export const enum APIVersion {
export const enum API {
V1 = 'v1',
REST = 'rest',
}

export const enum APIVersion {
// Experimental API versions
V20210811exp = '2021-08-11~experimental',
V20220216exp = '2022-02-16~experimental',

// GA API versions
V20230529 = '2023-05-29',
V20231103 = '2023-11-03',
}
4 changes: 2 additions & 2 deletions src/lib/utils/OAuth2Strategy/OAuth2Strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getOAuth2(): OAuth2Strategy {
// Note: the value of version being manually added
return new OAuth2Strategy(
{
authorizationURL: `${APP_BASE}${config.get(Config.AuthURL)}?version=2021-08-11~experimental`,
authorizationURL: `${APP_BASE}${config.get(Config.AuthURL)}?version=${APIVersion.V20210811exp}`,
tokenURL: `${API_BASE}${config.get(Config.TokenURL)}`,
clientID: clientID,
clientSecret: clientSecret,
Expand All @@ -64,7 +64,7 @@ export function getOAuth2(): OAuth2Strategy {
* information for profile management, but for demo purposes we are
* using the V1 endpoint.
*/
const response = await callSnykApi('bearer', access_token, APIVersion.V1).get('/user/me');
const response = await callSnykApi('bearer', access_token).get('/user/me');
const userId = response.data.id;
const { expires_in, scope, token_type } = params;
/**
Expand Down
12 changes: 5 additions & 7 deletions src/lib/utils/api/callSnykApi.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import axios, { AxiosInstance } from 'axios';
import { APIVersion } from '../../types';
import { API } from '../../types';
import { API_BASE } from '../../../app';
import { refreshTokenReqInterceptor, refreshTokenRespInterceptor } from './interceptors';

/**
* Utility function to call the Snyk API
* @param {String} tokenType ex: bearer, token, etc
* @param {String} token authentication token
* @param {APIVersion} version API version to call
* @returns {AxiosInstance}
* @see {@link https://snyk.docs.apiary.io/ V1 API Docs}
*/
export function callSnykApi(tokenType: string, token: string, version: APIVersion): AxiosInstance {
const contentType = version === APIVersion.V1 ? 'application/json' : 'application/vnd.api+json';

export function callSnykApi(tokenType: string, token: string): AxiosInstance {
const axiosInstance = axios.create({
baseURL: `${API_BASE}/${version}`,
baseURL: `${API_BASE}/${API.V1}`,
headers: {
'Content-Type': contentType,
'Content-Type': 'application/json',
Authorization: `${tokenType} ${token}`,
},
});
Expand Down
14 changes: 6 additions & 8 deletions src/lib/utils/api/callSnykRestApi.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import axios, { AxiosInstance } from 'axios';
import { APIVersion } from '../../types';
import { API } from '../../types';
import { API_BASE } from '../../../app';
import { refreshTokenReqInterceptor, refreshTokenRespInterceptor } from './interceptors';

/**
* Utility function to call the Snyk API
* Utility function to call the Snyk REST API
* @param {String} tokenType ex: bearer, token, etc
* @param {String} token authentication token
* @param {APIVersion} version API version to call
* @see {@link https://apidocs.snyk.io/ REST API Docs}
* @returns {AxiosInstance}
*/
export function callSnykRestApi(tokenType: string, token: string, version: APIVersion): AxiosInstance {
const contentType = version === APIVersion.REST ? 'application/vnd.api+json' : 'application/json';

export function callSnykRestApi(tokenType: string, token: string): AxiosInstance {
const axiosInstance = axios.create({
baseURL: `${API_BASE}/${version}`,
baseURL: `${API_BASE}/${API.REST}`,
headers: {
'Content-Type': contentType,
'Content-Type': 'application/vnd.api+json',
Authorization: `${tokenType} ${token}`,
},
});
Expand Down
3 changes: 1 addition & 2 deletions src/lib/utils/apiRequests/getAppOrg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export async function getAppOrgs(tokenType: string, accessToken: string): Promis
const result = await callSnykApi(
tokenType,
accessToken,
APIVersion.V1,
)({
method: 'GET',
url: `/orgs?version=2022-02-16~experimental`,
url: `/orgs?version=${APIVersion.V20220216exp}`,
});

return {
Expand Down
15 changes: 11 additions & 4 deletions src/scripts/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import yargs from 'yargs/yargs';
import axios from 'axios';
import fs from 'fs';
import { API_BASE } from '../app';
import { APIVersion } from '../lib/types';
import { v4 as uuidv4 } from 'uuid';

type Args = {
Expand Down Expand Up @@ -36,11 +37,17 @@ const args = yargs(process.argv.slice(2)).options({
async function createApp(args: Args) {
return axios({
method: 'POST',
url: `${API_BASE}/rest/orgs/${args.orgId}/apps?version=2022-03-11~experimental`,
url: `${API_BASE}/rest/orgs/${args.orgId}/apps/creations?version=${APIVersion.V20231103}`,
data: {
redirect_uris: args.redirectUris || ['http://localhost:3000/callback'],
scopes: args.scopes,
name: args.name,
data: {
attributes: {
context: 'tenant',
name: args.name,
redirect_uris: args.redirectUris || ['http://localhost:3000/callback'],
scopes: args.scopes,
},
type: 'app',
},
},
headers: {
authorization: `token ${args.authToken}`,
Expand Down

0 comments on commit 9cccba6

Please sign in to comment.