feat: add api package
This commit is contained in:
77
packages/api/src/Domain/Client/User/UserApiService.spec.ts
Normal file
77
packages/api/src/Domain/Client/User/UserApiService.spec.ts
Normal 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()
|
||||
})
|
||||
})
|
||||
45
packages/api/src/Domain/Client/User/UserApiService.ts
Normal file
45
packages/api/src/Domain/Client/User/UserApiService.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
}
|
||||
2
packages/api/src/Domain/Client/index.ts
Normal file
2
packages/api/src/Domain/Client/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './User/UserApiService'
|
||||
export * from './User/UserApiServiceInterface'
|
||||
Reference in New Issue
Block a user