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

Create app command #82

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Flags } from '@oclif/core';

import { AuthenticatedCommand } from 'commands-base/authenticated-command';
import { createApp } from 'services/apps-service';
import logger from 'utils/logger';

export default class AppCreate extends AuthenticatedCommand {
maorb-dev marked this conversation as resolved.
Show resolved Hide resolved
static description = 'Create an app.';

static withPrintCommand = false;

static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> -n NEW_APP_NAME'];

static flags = AppCreate.serializeFlags({
port: Flags.integer({
char: 'n',
description: 'Name your new app.',
required: false,
}),
});

DEBUG_TAG = 'app_create';

public async run(): Promise<void> {
try {
const { flags } = await this.parse(AppCreate);
const { name } = flags;

logger.debug(`invoking create app: name=${name}`, this.DEBUG_TAG);
const app = await createApp();
logger.success(`App created successfully: ${app.name} (id: ${app.id})`);
} catch (error: any) {
logger.debug(error, this.DEBUG_TAG);

// need to signal to the parent process that the command failed
process.exit(1);
}
}
}
4 changes: 4 additions & 0 deletions src/consts/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const listAppsUrl = (): string => {
return BASE_APPS_URL;
};

export const createAppUrl = (): string => {
return BASE_APPS_URL;
};

export const listAppVersionsByAppIdUrl = (appId: AppId): string => {
return `${BASE_APPS_URL}/${appId}/versions`;
};
Expand Down
30 changes: 27 additions & 3 deletions src/services/apps-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { listAppsUrl } from 'consts/urls';
import { createAppUrl, listAppsUrl } from 'consts/urls';
import { execute } from 'services/api-service';
import { listAppSchema } from 'services/schemas/apps-service-schemas';
import { createAppSchema, listAppSchema } from 'services/schemas/apps-service-schemas';
import { HttpError } from 'types/errors';
import { HttpMethodTypes } from 'types/services/api-service';
import { App, ListAppResponse } from 'types/services/apps-service';
import { App, CreateAppResponse, ListAppResponse } from 'types/services/apps-service';
import { appsUrlBuilder } from 'utils/urls-builder';

export const listApps = async (): Promise<Array<App>> => {
Expand All @@ -28,3 +28,27 @@ export const listApps = async (): Promise<Array<App>> => {
throw new Error('Failed to list apps.');
}
};

export const createApp = async (name?: string): Promise<App> => {
try {
const path = createAppUrl();
const url = appsUrlBuilder(path);
const response = await execute<CreateAppResponse>(
{
url,
headers: { Accept: 'application/json' },
method: HttpMethodTypes.POST,
body: name ? { name } : undefined,
},
createAppSchema,
);

return response.app;
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
}

throw new Error('Failed to create app.');
}
};
6 changes: 6 additions & 0 deletions src/services/schemas/apps-service-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export const listAppSchema = z
apps: z.array(appSchema),
})
.merge(baseResponseHttpMetaDataSchema);

export const createAppSchema = z
.object({
app: appSchema,
})
.merge(baseResponseHttpMetaDataSchema);
3 changes: 2 additions & 1 deletion src/types/services/apps-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';

import { appSchema, listAppSchema } from 'services/schemas/apps-service-schemas';
import { appSchema, createAppSchema, listAppSchema } from 'services/schemas/apps-service-schemas';

export type App = z.infer<typeof appSchema>;
export type ListAppResponse = z.infer<typeof listAppSchema>;
export type CreateAppResponse = z.infer<typeof createAppSchema>;
Loading