Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/actions/auth/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ export class StatusAction {

public async execute(authKey: string | null): Promise<ActionResult> {
const accountInfo = await getAuthInfo(this.configDir.toString());
if (accountInfo === null) {
// `auth logout` blanks config.json rather than deleting it, so a logged-out user still
// has a non-null AuthInfo with an empty key. Checking the key catches both that and a
// missing config file, and avoids a request that can only come back 401.
if (!accountInfo?.authKey) {
this.prompts.notLoggedIn();
return ActionResult.failed();
}
const result = await this.prompts.accountInfoSpinner(
Expand Down
18 changes: 12 additions & 6 deletions src/infrastructure/services/publishing-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export class PublishingApiService {
shell: string
): Promise<Result<PublishingProfileItem[], ServiceError>> {
const authInfo: AuthInfo | null = await getAuthInfo(configDir.toString());
if (authInfo === null) {
return err(ServiceError.UnAuthorized);
// `auth logout` blanks config.json rather than deleting it, so a logged-out user
// still has a non-null AuthInfo with an empty key — check the key, not the object.
if (!authInfo?.authKey) {
return err(ServiceError.unauthorizedWithHint(null));
}

try {
Expand All @@ -51,8 +53,10 @@ export class PublishingApiService {
shell: string
): Promise<Result<PublishingInfo, ServiceError>> {
const authInfo: AuthInfo | null = await getAuthInfo(configDir.toString());
if (authInfo === null) {
return err(ServiceError.UnAuthorized);
// `auth logout` blanks config.json rather than deleting it, so a logged-out user
// still has a non-null AuthInfo with an empty key — check the key, not the object.
if (!authInfo?.authKey) {
return err(ServiceError.unauthorizedWithHint(null));
}
const sdkFileStream = await this.fileService.getStream(sdkFilePath);

Expand Down Expand Up @@ -107,8 +111,10 @@ export class PublishingApiService {
shell: string
): Promise<Result<PublishLogItem, ServiceError>> {
const authInfo: AuthInfo | null = await getAuthInfo(configDir.toString());
if (authInfo === null) {
return err(ServiceError.UnAuthorized);
// `auth logout` blanks config.json rather than deleting it, so a logged-out user
// still has a non-null AuthInfo with an empty key — check the key, not the object.
if (!authInfo?.authKey) {
return err(ServiceError.unauthorizedWithHint(null));
}

try {
Expand Down
3 changes: 2 additions & 1 deletion src/infrastructure/services/transformation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { apiClientFactory } from "./api-client-factory.js";
import { FilePath } from "../../types/file/filePath.js";
import { CommandMetadata } from "../../types/common/command-metadata.js";
import { err, ok, Result} from "neverthrow";
import { ServiceError } from "../service-error.js";

export interface TransformViaFileParams {
file: FilePath;
Expand Down Expand Up @@ -86,7 +87,7 @@ export class TransformationService {
return "Your API Definition is invalid. Please use the APIMatic VS Code Extension to fix the errors and try again.";
} else if (apiError.statusCode === 401) {
const message = JSON.parse(apiError.body as string).message;
return `${message} You are not authorized to perform this action. Please run 'auth:login' or provide a valid auth key.`;
return ServiceError.unauthorizedWithHint(message).errorMessage;
}
return `Error ${apiError.statusCode}: An error occurred during the transformation. Please try again or contact [email protected] for assistance.`;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/services/validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class ValidationService {
case 400:
return "Your API Definition is invalid. Please fix the issues and try again.";
case 401:
return "You are not authorized to perform this action. Please run 'auth:login' or provide a valid auth key.";
return ServiceError.unauthorizedWithHint(null).errorMessage;
case 403:
return "You do not have permission to perform this action.";
case 500:
Expand Down
5 changes: 5 additions & 0 deletions src/prompts/auth/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ export class StatusPrompts {
);
}

public notLoggedIn() {
log.error(`You are not logged in. Please run ${format.cmdAlt("apimatic", "auth", "login")} to log in.`);
}

public invalidKeyProvided(serviceError: ServiceError) {
const message =
serviceError === ServiceError.UnAuthorized ? "Invalid API key provided." : serviceError.errorMessage;
log.error(message);
}


public showAccountInfo(info: SubscriptionInfo) {
const languages = mapLanguages(info.allowedLanguages);
const message = `Account Information:
Expand Down
Loading