first commit

This commit is contained in:
Sergo
2023-07-29 21:10:00 +03:00
commit 27efd26d6c
49 changed files with 5490 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
import type { Params, ServiceInterface } from '@feathersjs/feathers'
import type { Application } from '../../declarations'
import wildDuckClient from '../../clients/wildduck.client'
import { faker } from '@faker-js/faker'
import { BadRequest } from '@feathersjs/errors'
interface Alias {
success: boolean,
id: string,
address: string,
main: boolean,
user: string,
tags: string[],
created: string,
}
interface GetAddressInfoResponse {
success: boolean,
results: Alias[]
}
interface CreateAddressResponse {
success: boolean,
id: string,
}
type AliasesData = any
type AliasesPatch = any
type AliasesQuery = any
export type { Alias as Aliases, AliasesData, AliasesPatch, AliasesQuery }
export interface AliasesServiceOptions {
app: Application
}
export interface AliasesParams extends Params<AliasesQuery> {
session?: any
}
export class AliasesService<ServiceParams extends AliasesParams = AliasesParams>
implements ServiceInterface<Alias, AliasesData, ServiceParams, AliasesPatch>
{
constructor(public options: AliasesServiceOptions) { }
async find(params: ServiceParams): Promise<Alias[]> {
const userId = await this.getUserIdByEmailAddress(params)
const { data: userAddressesResponse } = await wildDuckClient.get<GetAddressInfoResponse>(`/users/${userId}/addresses`)
return userAddressesResponse.results
}
async create(data: AliasesData, params: ServiceParams): Promise<Alias>
async create(data: AliasesData, params: ServiceParams): Promise<Alias | Alias[]> {
const userId = await this.getUserIdByEmailAddress(params)
const alias = `${faker.animal.crocodilia().replace(/\s/, '').slice(10)}-${faker.git.commitSha({ length: 5 })}`;
const createResult = await wildDuckClient.post<CreateAddressResponse>(`/users/${userId}/addresses`, {
address: alias
})
if (!createResult.data.success) {
throw new BadRequest('Failed to create alias')
}
const { data: userAddressesResponse } = await wildDuckClient.get<GetAddressInfoResponse>(`/users/${userId}/addresses`)
return userAddressesResponse.results
}
private async getUserIdByEmailAddress(params: ServiceParams): Promise<string> {
const emails = params.session?.user?.emails;
const addressInfoResponse = await Promise.any(emails.map((email: string) => wildDuckClient.get<Alias>(`addresses/resolve/${email}`)))
return addressInfoResponse.data.user
}
}
export const getOptions = (app: Application) => {
return { app }
}

View File

@@ -0,0 +1,41 @@
import type { Application } from '../../declarations'
import { validateAuth } from '../../hooks/validate-auth'
import { AliasesService, getOptions } from './aliases.class'
export const aliasesPath = 'aliases'
export const aliasesMethods = ['find', 'create'] as const
export * from './aliases.class'
export const aliases = (app: Application) => {
app.use(aliasesPath, new AliasesService(getOptions(app)), {
methods: aliasesMethods,
events: []
})
app.service(aliasesPath).hooks({
around: {
all: []
},
before: {
all: [
validateAuth
],
find: [],
create: [],
},
after: {
all: []
},
error: {
all: []
}
})
}
// Add this service to the service type index
declare module '../../declarations' {
interface ServiceTypes {
[aliasesPath]: AliasesService
}
}