feat: add api package

This commit is contained in:
Karol Sójko
2022-07-06 11:53:39 +02:00
parent e2e9a11984
commit 9d7d18e7f2
48 changed files with 827 additions and 10 deletions

View File

@@ -0,0 +1,77 @@
import { ProtocolVersion } from '@standardnotes/common'
import { RootKeyParamsInterface } from '@standardnotes/models'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
import { UserServerInterface } from '../../Server'
import { UserApiService } from './UserApiService'
describe('UserApiService', () => {
let userServer: UserServerInterface
let keyParams: RootKeyParamsInterface
const createService = () => new UserApiService(userServer)
beforeEach(() => {
userServer = {} as jest.Mocked<UserServerInterface>
userServer.register = jest.fn().mockReturnValue({
data: { user: { email: 'test@test.te', uuid: '1-2-3' } },
} as jest.Mocked<UserRegistrationResponse>)
keyParams = {} as jest.Mocked<RootKeyParamsInterface>
keyParams.getPortableValue = jest.fn().mockReturnValue({
identifier: 'test@test.te',
version: ProtocolVersion.V004,
})
})
it('should register a user', async () => {
const response = await createService().register('test@test.te', 'testpasswd', keyParams, false)
expect(response).toEqual({
data: {
user: {
email: 'test@test.te',
uuid: '1-2-3',
},
},
})
expect(userServer.register).toHaveBeenCalledWith({
api: '20200115',
email: 'test@test.te',
ephemeral: false,
identifier: 'test@test.te',
password: 'testpasswd',
version: '004',
})
})
it('should not register a user if it is already registering', async () => {
const service = createService()
Object.defineProperty(service, 'registering', {
get: () => true,
})
let error = null
try {
await service.register('test@test.te', 'testpasswd', keyParams, false)
} catch (caughtError) {
error = caughtError
}
expect(error).not.toBeNull()
})
it('should not register a user if the server fails', async () => {
userServer.register = jest.fn().mockImplementation(() => {
throw new Error('Oops')
})
let error = null
try {
await createService().register('test@test.te', 'testpasswd', keyParams, false)
} catch (caughtError) {
error = caughtError
}
expect(error).not.toBeNull()
})
})

View File

@@ -0,0 +1,45 @@
import { RootKeyParamsInterface } from '@standardnotes/models'
import { ErrorMessage } from '../../Error/ErrorMessage'
import { ApiCallError } from '../../Error/ApiCallError'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
import { UserServerInterface } from '../../Server/User/UserServerInterface'
import { ApiVersion } from '../../Api/ApiVersion'
import { ApiEndpointParam } from '../../Request/ApiEndpointParam'
import { UserApiServiceInterface } from './UserApiServiceInterface'
export class UserApiService implements UserApiServiceInterface {
private registering: boolean
constructor(private userServer: UserServerInterface) {
this.registering = false
}
async register(
email: string,
serverPassword: string,
keyParams: RootKeyParamsInterface,
ephemeral: boolean,
): Promise<UserRegistrationResponse> {
if (this.registering) {
throw new ApiCallError(ErrorMessage.RegistrationInProgress)
}
this.registering = true
try {
const response = await this.userServer.register({
[ApiEndpointParam.ApiVersion]: ApiVersion.v0,
password: serverPassword,
email,
ephemeral,
...keyParams.getPortableValue(),
})
this.registering = false
return response
} catch (error) {
throw new ApiCallError(ErrorMessage.GenericRegistrationFail)
}
}
}

View File

@@ -0,0 +1,11 @@
import { RootKeyParamsInterface } from '@standardnotes/models'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
export interface UserApiServiceInterface {
register(
email: string,
serverPassword: string,
keyParams: RootKeyParamsInterface,
ephemeral: boolean,
): Promise<UserRegistrationResponse>
}

View File

@@ -0,0 +1,2 @@
export * from './User/UserApiService'
export * from './User/UserApiServiceInterface'