Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support application templates #19

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"/oclif.manifest.json"
],
"dependencies": {
"@basis-theory/basis-theory-js": "^2.4.0",
"@basis-theory/basis-theory-js": "^2.7.0",
"@inquirer/checkbox": "^1.5.0",
"@inquirer/confirm": "^2.0.15",
"@inquirer/input": "^1.2.14",
Expand All @@ -31,6 +31,7 @@
"dotenv": "^16.3.1",
"fastify": "^4.19.2",
"lodash.debounce": "^4.0.8",
"lodash.groupby": "^4.6.0",
"pino": "^8.14.1",
"pino-pretty": "^10.0.0",
"serialize-error": "^11.0.0",
Expand All @@ -49,6 +50,7 @@
"@types/debug": "^4.1.8",
"@types/detect-port": "^1.3.3",
"@types/lodash.debounce": "^4.0.7",
"@types/lodash.groupby": "^4.6.9",
"@types/mocha": "^9.0.0",
"@types/node": "^16.18.36",
"chai": "^4",
Expand Down
52 changes: 52 additions & 0 deletions src/applications/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import type {
Permission,
UpdateApplication,
} from '@basis-theory/basis-theory-js/types/models';
import { ApplicationTemplate } from '@basis-theory/basis-theory-js/types/models/application-templates';
import type {
BasisTheory as IBasisTheory,
PaginatedList,
} from '@basis-theory/basis-theory-js/types/sdk';
import confirm from '@inquirer/confirm';
import select from '@inquirer/select';
import { ux } from '@oclif/core';
import groupBy from 'lodash.groupby';
import { TableRow } from '../types';
import { selectOrNavigate } from '../utils';

Expand Down Expand Up @@ -146,10 +149,59 @@ const deleteApplication = async (
return true;
};

const createApplicationFromTemplate = async (
bt: IBasisTheory,
templateId: string
): Promise<Application> => {
const template = await bt.applicationTemplates.retrieve(templateId);

return createApplication(bt, {
name: template.name,
type: template.applicationType,
permissions: template.permissions,
rules: template.rules,
});
};

const promptTemplate = async (
bt: IBasisTheory
): Promise<ApplicationTemplate | undefined> => {
const useTemplate = await confirm({
message: 'Do you want to use an application template?',
});

if (!useTemplate) {
return undefined;
}

const templates = groupBy(
await bt.applicationTemplates.list(),
'templateType'
);

const type = await select({
message: 'Which template type do you want to use?',
choices: Object.keys(templates).map((t) => ({
value: t,
})),
});

return select({
message: 'Choose a template',
choices: templates[type].map((t) => ({
name: t.name,
value: t,
description: t.description,
})),
});
};

export {
listPermissions,
createApplication,
updateApplication,
selectApplication,
deleteApplication,
createApplicationFromTemplate,
promptTemplate,
};
76 changes: 53 additions & 23 deletions src/commands/applications/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
import { Flags } from '@oclif/core';
import {
createApplication,
createApplicationFromTemplate,
listPermissions,
promptTemplate,
} from '../../applications/management';
import { APPLICATION_FLAGS } from '../../applications/utils';
import { BaseCommand } from '../../base';
Expand All @@ -28,39 +30,67 @@ export default class Create extends BaseCommand {
description: 'type of the Application',
options: [...APPLICATION_TYPES],
}),
template: Flags.string({
description: 'template ID to create the application with',
char: 'z',
}),
};

public async run(): Promise<void> {
const { flags, bt } = await this.parse(Create);

const name = await promptStringIfUndefined(flags.name, {
message: 'What is the Application name?',
validate: (value) => Boolean(value),
});
let application;

if (flags.template) {
// template passed as flag
application = await createApplicationFromTemplate(bt, flags.template);
} else {
let template;
let rules, permissions, type;

if (!flags.name && !flags.type && !flags.permission) {
// no other flags where passed, prompt template
template = await promptTemplate(bt);
}

const name = await promptStringIfUndefined(flags.name, {
message: 'What is the Application name?',
default: template?.name,
validate: (value) => Boolean(value),
});

const type = (await promptSelectIfUndefined(flags.type, {
message: 'What is the Application type?',
choices: APPLICATION_TYPES.map((t) => ({ value: t })),
})) as ApplicationType;
if (template) {
// template has been picked, so type and permissions/rules are defined
type = template.applicationType;
rules = template.rules;
permissions = template.permissions;
} else {
type = (await promptSelectIfUndefined(flags.type, {
message: 'What is the Application type?',
choices: APPLICATION_TYPES.map((t) => ({ value: t })),
})) as ApplicationType;

const available = await listPermissions(bt, type);
const available = await listPermissions(bt, type);

const permissions = await promptCheckboxIfUndefined(flags.permission, {
message: 'Select the permissions for the Application',
choices: available.map((p) => ({
value: p.type,
})),
loop: false,
});
permissions = await promptCheckboxIfUndefined(flags.permission, {
message: 'Select the permissions for the Application',
choices: available.map((p) => ({
value: p.type,
})),
loop: false,
});
}

const { id, key } = await createApplication(bt, {
name,
type,
permissions,
});
application = await createApplication(bt, {
name,
type,
permissions,
rules,
});
}

this.log('Application created successfully!');
this.log(`id: ${id}`);
this.log(`key: ${key}`);
this.log(`id: ${application.id}`);
this.log(`key: ${application.key}`);
}
}
48 changes: 30 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@
"@babel/helper-validator-identifier" "^7.22.5"
to-fast-properties "^2.0.0"

"@basis-theory/basis-theory-js@^2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@basis-theory/basis-theory-js/-/basis-theory-js-2.4.0.tgz#eaf2e8770dc1f829151ef111b520c3c831113ffc"
integrity sha512-+duvyquf7lXHkytrdYxsJ2dTASlaqDpDgv2yk1M00WRHfzLNyEW5ZPyAPsG7/a4i3bbs4BgZLrvO3MeMJAjtgw==
"@basis-theory/basis-theory-js@^2.7.0":
version "2.7.0"
resolved "https://registry.yarnpkg.com/@basis-theory/basis-theory-js/-/basis-theory-js-2.7.0.tgz#0a1b439fdcfd4144ff71b52467a8a21da9559cae"
integrity sha512-x726u3SRgyMtofA/tn3WS7Bma1M4JOGI+f1sseESVUu5xrk8gftGqU0fxIKYNUZlAP39WXsmo8fpRzNZ9peO3Q==
dependencies:
axios "^1.6.0"
camelcase-keys "^6.2.2"
Expand Down Expand Up @@ -1597,6 +1597,13 @@
dependencies:
"@types/lodash" "*"

"@types/lodash.groupby@^4.6.9":
version "4.6.9"
resolved "https://registry.yarnpkg.com/@types/lodash.groupby/-/lodash.groupby-4.6.9.tgz#ea1aa9da1038ca50894d1fe1a3b5dabdf865d99c"
integrity sha512-z2xtCX2ko7GrqORnnYea4+ksT7jZNAvaOcLd6mP9M7J09RHvJs06W8BGdQQAX8ARef09VQLdeRilSOcfHlDQJQ==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.178"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.178.tgz"
Expand Down Expand Up @@ -4568,9 +4575,9 @@ get-caller-file@^2.0.5:
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==

get-func-name@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/get-func-name/-/get-func-name-2.0.0.tgz"
integrity sha1-6td0q+5y4gQJQzoGY2YCPdaIekE=
version "2.0.2"
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.2.tgz#0d7cf20cd13fda808669ffa88f4ffc7a3943fc41"
integrity sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==

get-intrinsic@^1.0.2, get-intrinsic@^1.1.1, get-intrinsic@^1.1.3, get-intrinsic@^1.2.0:
version "1.2.1"
Expand Down Expand Up @@ -5964,6 +5971,11 @@ lodash.escaperegexp@^4.1.2:
resolved "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz"
integrity sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==

lodash.groupby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
integrity sha512-5dcWxm23+VAoz+awKmBaiBvzox8+RqMgFhi7UvX9DHZr2HdxHXM/Wrf8cfKpsW37RNrvtPn6hSwNqurSILbmJw==

lodash.ismatch@^4.4.0:
version "4.4.0"
resolved "https://registry.npmjs.org/lodash.ismatch/-/lodash.ismatch-4.4.0.tgz"
Expand Down Expand Up @@ -6517,10 +6529,10 @@ [email protected]:
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.1.tgz"
integrity sha512-n6Vs/3KGyxPQd6uO0eH4Bv0ojGSUvuLlIHtC3Y0kEO23YRge8H9x1GCzLn28YX0H66pMkxuaeESFq4tKISKwdw==

nanoid@^3.3.6:
version "3.3.6"
resolved "https://registry.npmjs.org/nanoid/-/nanoid-3.3.6.tgz"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
nanoid@^3.3.7:
version "3.3.7"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.7.tgz#d0c301a691bc8d54efa0a2226ccf3fe2fd656bd8"
integrity sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==

natural-compare@^1.4.0:
version "1.4.0"
Expand Down Expand Up @@ -7552,11 +7564,11 @@ postcss-selector-parser@^6.0.10:
util-deprecate "^1.0.2"

postcss@^8.1.10:
version "8.4.24"
resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.24.tgz"
integrity sha512-M0RzbcI0sO/XJNucsGjvWU9ERWxb/ytp1w6dKtxTKgixdtQDq4rmx/g8W1hnaheq9jgwL/oyEdH5Bc4WwJKMqg==
version "8.4.32"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.32.tgz#1dac6ac51ab19adb21b8b34fd2d93a86440ef6c9"
integrity sha512-D/kj5JNu6oo2EIy+XL/26JEDTlIbB8hw85G8StOE6L74RQAVVP5rej6wxCNqyMbR4RkPfqvezVbPw81Ngd6Kcw==
dependencies:
nanoid "^3.3.6"
nanoid "^3.3.7"
picocolors "^1.0.0"
source-map-js "^1.0.2"

Expand Down Expand Up @@ -9455,9 +9467,9 @@ widest-line@^3.1.0:
string-width "^4.0.0"

word-wrap@^1.2.3:
version "1.2.3"
resolved "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz"
integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==
version "1.2.5"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==

wordwrap@^1.0.0:
version "1.0.0"
Expand Down