add prettier

This commit is contained in:
2023-08-06 09:21:17 +03:00
parent cc453e2337
commit feb5e5b4ca
25 changed files with 524 additions and 591 deletions

View File

@@ -1,186 +1,142 @@
import type {
NullableId,
Params,
ServiceInterface,
} from "@feathersjs/feathers";
import type { NullableId, Params, ServiceInterface } from '@feathersjs/feathers';
import type { Application } from "../../declarations";
import wildDuckClient from "../../clients/wildduck.client";
import { faker, th } from "@faker-js/faker";
import { BadRequest } from "@feathersjs/errors";
import config from "config";
import type { Application } from '../../declarations';
import wildDuckClient from '../../clients/wildduck.client';
import { faker, th } from '@faker-js/faker';
import { BadRequest } from '@feathersjs/errors';
import config from 'config';
interface WildDuckAddress {
success: boolean;
id: string;
address: string;
main: boolean;
user: string;
tags: string[];
created: string;
success: boolean;
id: string;
address: string;
main: boolean;
user: string;
tags: string[];
created: string;
}
interface GetWildDuckAddressInfoResponse {
success: boolean;
results: WildDuckAddress[];
success: boolean;
results: WildDuckAddress[];
}
interface AliasApiResponse {
id: string | null;
address: string;
tags: string[];
created: string;
id: string | null;
address: string;
tags: string[];
created: string;
}
interface CreateWildDuckAddressResponse {
success: boolean;
id: string;
success: boolean;
id: string;
}
type AliasesData = any;
type AliasesPatch = any;
type AliasesQuery = any;
export type {
WildDuckAddress as Aliases,
AliasesData,
AliasesPatch,
AliasesQuery,
};
export type { WildDuckAddress as Aliases, AliasesData, AliasesPatch, AliasesQuery };
export interface AliasesServiceOptions {
app: Application;
app: Application;
}
export interface AliasesParams extends Params<AliasesQuery> {
session?: any;
session?: any;
}
export class AliasesService<ServiceParams extends AliasesParams = AliasesParams>
implements
ServiceInterface<
AliasApiResponse,
AliasesData,
ServiceParams,
AliasesPatch
>
implements ServiceInterface<AliasApiResponse, AliasesData, ServiceParams, AliasesPatch>
{
constructor(public options: AliasesServiceOptions) {}
constructor(public options: AliasesServiceOptions) {}
async find(params: ServiceParams): Promise<AliasApiResponse[]> {
const userId = await this.getUserIdByEmailAddress(params);
async find(params: ServiceParams): Promise<AliasApiResponse[]> {
const userId = await this.getUserIdByEmailAddress(params);
return this.getUserAddresses(userId);
}
async create(
data: AliasesData,
params: ServiceParams,
): Promise<AliasApiResponse>;
async create(
data: AliasesData,
params: ServiceParams,
): Promise<AliasApiResponse | AliasApiResponse[]> {
const userId = await this.getUserIdByEmailAddress(params);
const randomString = faker.git.commitSha({ length: 4 });
// Replace all non-alphanumeric characters with nothing and spaces with dashes
const alias =
`${faker.color.human()}-${faker.animal.snake()}-${randomString}`
.replace(/\s+/g, "-")
.replace(/[^a-zA-Z0-9-]/g, "")
.toLowerCase();
const emailDomain = config.get("wildDuck.domain");
const createResult =
await wildDuckClient.post<CreateWildDuckAddressResponse>(
`/users/${userId}/addresses`,
{
address: `${alias}@${emailDomain}`,
},
);
if (!createResult.data.success) {
throw new BadRequest("Failed to create alias");
return this.getUserAddresses(userId);
}
return this.getUserAddresses(userId);
}
async create(data: AliasesData, params: ServiceParams): Promise<AliasApiResponse>;
async create(data: AliasesData, params: ServiceParams): Promise<AliasApiResponse | AliasApiResponse[]> {
const userId = await this.getUserIdByEmailAddress(params);
private async getUserIdByEmailAddress(
params: ServiceParams,
): Promise<string> {
const emails = params.session?.user?.emails;
const randomString = faker.git.commitSha({ length: 4 });
const preferredDomain = config.get("wildDuck.preferredDomain");
// Replace all non-alphanumeric characters with nothing and spaces with dashes
const alias = `${faker.color.human()}-${faker.animal.snake()}-${randomString}`
.replace(/\s+/g, '-')
.replace(/[^a-zA-Z0-9-]/g, '')
.toLowerCase();
if (!emails.length || !preferredDomain) {
throw new BadRequest("Unable to find user");
const emailDomain = config.get('wildDuck.domain');
const createResult = await wildDuckClient.post<CreateWildDuckAddressResponse>(`/users/${userId}/addresses`, {
address: `${alias}@${emailDomain}`,
});
if (!createResult.data.success) {
throw new BadRequest('Failed to create alias');
}
return this.getUserAddresses(userId);
}
const addressInfoResponse = await Promise.any(
emails
.filter((email: string) =>
email.endsWith(config.get("wildDuck.preferredDomain")),
)
.map((email: string) =>
wildDuckClient.get<WildDuckAddress>(`addresses/resolve/${email}`),
),
);
private async getUserIdByEmailAddress(params: ServiceParams): Promise<string> {
const emails = params.session?.user?.emails;
return addressInfoResponse.data.user;
}
const preferredDomain = config.get('wildDuck.preferredDomain');
private async getUserAddresses(userId: string): Promise<AliasApiResponse[]> {
const { data: userAddressesResponse } =
await wildDuckClient.get<GetWildDuckAddressInfoResponse>(
`/users/${userId}/addresses`,
);
if (!emails.length || !preferredDomain) {
throw new BadRequest('Unable to find user');
}
return userAddressesResponse.results.map(this.sanitizeAliasResponse);
}
const addressInfoResponse = await Promise.any(
emails
.filter((email: string) => email.endsWith(config.get('wildDuck.preferredDomain')))
.map((email: string) => wildDuckClient.get<WildDuckAddress>(`addresses/resolve/${email}`))
);
async remove(
id: NullableId,
params: ServiceParams,
): Promise<AliasApiResponse[]> {
const { data: addressInfoResponse } =
await wildDuckClient.get<WildDuckAddress>(`addresses/resolve/${id}`);
const allowedDomain: string = config.get("wildDuck.domain");
// If address does not match the allowed domain, throw an error
if (
!allowedDomain ||
!addressInfoResponse.address.endsWith(allowedDomain)
) {
throw new BadRequest("Unable to delete address");
return addressInfoResponse.data.user;
}
const userId = await this.getUserIdByEmailAddress(params);
await wildDuckClient.delete<WildDuckAddress>(
`users/${userId}/addresses/${id}`,
);
private async getUserAddresses(userId: string): Promise<AliasApiResponse[]> {
const { data: userAddressesResponse } = await wildDuckClient.get<GetWildDuckAddressInfoResponse>(
`/users/${userId}/addresses`
);
return this.getUserAddresses(userId);
}
return userAddressesResponse.results.map(this.sanitizeAliasResponse);
}
sanitizeAliasResponse(alias: WildDuckAddress): AliasApiResponse {
// Hide the id if the alias is not removable
const isRemovable =
alias.main ||
!alias.address.endsWith(config.get("wildDuck.preferredDomain"));
async remove(id: NullableId, params: ServiceParams): Promise<AliasApiResponse[]> {
const { data: addressInfoResponse } = await wildDuckClient.get<WildDuckAddress>(`addresses/resolve/${id}`);
const allowedDomain: string = config.get('wildDuck.domain');
return {
id: isRemovable ? null : alias.id,
address: alias.address,
tags: alias.tags,
created: alias.created,
};
}
// If address does not match the allowed domain, throw an error
if (!allowedDomain || !addressInfoResponse.address.endsWith(allowedDomain)) {
throw new BadRequest('Unable to delete address');
}
const userId = await this.getUserIdByEmailAddress(params);
await wildDuckClient.delete<WildDuckAddress>(`users/${userId}/addresses/${id}`);
return this.getUserAddresses(userId);
}
sanitizeAliasResponse(alias: WildDuckAddress): AliasApiResponse {
// Hide the id if the alias is not removable
const isRemovable = alias.main || !alias.address.endsWith(config.get('wildDuck.preferredDomain'));
return {
id: isRemovable ? null : alias.id,
address: alias.address,
tags: alias.tags,
created: alias.created,
};
}
}
export const getOptions = (app: Application) => {
return { app };
return { app };
};