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,9 @@
const UserPaths = {
register: '/v1/users',
}
export const Paths = {
v1: {
...UserPaths,
},
}

View File

@@ -0,0 +1,39 @@
import { ProtocolVersion } from '@standardnotes/common'
import { ApiVersion } from '../../Api'
import { HttpServiceInterface } from '../../Http'
import { UserRegistrationResponse } from '../../Response'
import { UserServer } from './UserServer'
describe('UserServer', () => {
let httpService: HttpServiceInterface
const createServer = () => new UserServer(httpService)
beforeEach(() => {
httpService = {} as jest.Mocked<HttpServiceInterface>
httpService.post = jest.fn().mockReturnValue({
data: { user: { email: 'test@test.te', uuid: '1-2-3' } },
} as jest.Mocked<UserRegistrationResponse>)
})
it('should register a user', async () => {
const response = await createServer().register({
password: 'test',
api: ApiVersion.v0,
email: 'test@test.te',
ephemeral: false,
version: ProtocolVersion.V004,
pw_nonce: 'test',
identifier: 'test@test.te',
})
expect(response).toEqual({
data: {
user: {
email: 'test@test.te',
uuid: '1-2-3',
},
},
})
})
})

View File

@@ -0,0 +1,15 @@
import { HttpServiceInterface } from '../../Http/HttpServiceInterface'
import { UserRegistrationRequestParams } from '../../Request/User/UserRegistrationRequestParams'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
import { Paths } from './Paths'
import { UserServerInterface } from './UserServerInterface'
export class UserServer implements UserServerInterface {
constructor(private httpService: HttpServiceInterface) {}
async register(params: UserRegistrationRequestParams): Promise<UserRegistrationResponse> {
const response = await this.httpService.post(Paths.v1.register, params)
return response as UserRegistrationResponse
}
}

View File

@@ -0,0 +1,6 @@
import { UserRegistrationRequestParams } from '../../Request/User/UserRegistrationRequestParams'
import { UserRegistrationResponse } from '../../Response/User/UserRegistrationResponse'
export interface UserServerInterface {
register(params: UserRegistrationRequestParams): Promise<UserRegistrationResponse>
}

View File

@@ -0,0 +1,3 @@
export * from './User/Paths'
export * from './User/UserServer'
export * from './User/UserServerInterface'