Skip to content

Commit

Permalink
Use shorter naming convention for conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Oct 7, 2024
1 parent cf04641 commit 00c61eb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/api/auth/lighthouse/LighthouseAuthApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class LighthouseAuthApi implements AuthApi {
async signUp(signup: Signup): Promise<Result<User>> {
try {
const apiResponse = await this.apiClient.register.registerCreate(
convert.signupToApiRegisterPayload(signup)
convert.signupToApi(signup)
);
const apiUser = apiResponse.data;
this.apiUser = apiUser;
return okResult(convert.apiUserToUser(apiUser));
return okResult(convert.userFromApi(apiUser));
} catch (error) {
return errorResult(`Signup failed: ${await formatError(error)}`);
}
Expand All @@ -40,11 +40,11 @@ export class LighthouseAuthApi implements AuthApi {
async logIn(login?: Login): Promise<Result<User>> {
try {
const apiUserResponse = await this.apiClient.login.loginCreate(
convert.loginToApiLoginPayload(login)
convert.loginToApi(login)
);
const apiUser = apiUserResponse.data;
this.apiUser = apiUser;
return okResult(convert.apiUserToUser(apiUser));
return okResult(convert.userFromApi(apiUser));
} catch (error) {
return errorResult(`Login failed: ${await formatError(error)}`);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ export class LighthouseAuthApi implements AuthApi {
// TODO: Implement pagination in Heimdall and remove this
apiUsers = slicePage(apiUsers, pagination);

return okResult(apiUsers.map(convert.apiUserToUser));
return okResult(apiUsers.map(convert.userFromApi));
} catch (error) {
return errorResult(
`Fetching all users failed: ${await formatError(error)}`
Expand All @@ -94,7 +94,7 @@ export class LighthouseAuthApi implements AuthApi {
apiUser.id
);
const apiToken: generated.APIToken = apiTokenResponse.data;
return okResult(convert.apiTokenToToken(apiToken));
return okResult(convert.tokenFromApi(apiToken));
} catch (error) {
return errorResult(`Fetching token failed: ${await formatError(error)}`);
}
Expand Down
14 changes: 6 additions & 8 deletions src/api/auth/lighthouse/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {
RegistrationKey,
} from '@luna/api/auth/types';

export function loginToApiLoginPayload(login?: Login): generated.LoginPayload {
export function loginToApi(login?: Login): generated.LoginPayload {
return {
username: login?.username,
password: login?.password,
};
}

export function signupToApiRegisterPayload(
signup: Signup
): generated.RegisterPayload {
export function signupToApi(signup: Signup): generated.RegisterPayload {
return {
registration_key: signup.registrationKey,
email: signup.email,
Expand All @@ -25,14 +23,14 @@ export function signupToApiRegisterPayload(
};
}

export function apiTokenToToken(apiToken: generated.APIToken): Token {
export function tokenFromApi(apiToken: generated.APIToken): Token {
return {
value: apiToken.api_token!,
expiresAt: apiToken.expires_at ? new Date(apiToken.expires_at) : undefined,
};
}

export function apiUserToUser(apiUser: generated.User): User {
export function userFromApi(apiUser: generated.User): User {
return {
id: apiUser.id,
username: apiUser.username!,
Expand All @@ -43,12 +41,12 @@ export function apiUserToUser(apiUser: generated.User): User {
lastSeen: apiUser.last_login ? new Date(apiUser.last_login) : undefined,
permanentApiToken: apiUser.permanent_api_token,
registrationKey: apiUser.registration_key
? apiRegistrationKeyToRegistrationKey(apiUser.registration_key)
? registrationKeyFromApi(apiUser.registration_key)
: undefined,
};
}

export function apiRegistrationKeyToRegistrationKey(
export function registrationKeyFromApi(
registrationKey: generated.RegistrationKey
): RegistrationKey {
return {
Expand Down

0 comments on commit 00c61eb

Please sign in to comment.