refactor: http service (#2233)
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { ChangeCredentialsData } from './ChangeCredentialsData'
|
||||
|
||||
export type ChangeCredentialsResponse = HttpResponse & {
|
||||
data: ChangeCredentialsData
|
||||
}
|
||||
export type ChangeCredentialsResponse = ChangeCredentialsData
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { KeyParamsData } from './KeyParamsData'
|
||||
|
||||
export type KeyParamsResponse = HttpResponse & {
|
||||
data: KeyParamsData
|
||||
}
|
||||
export type KeyParamsResponse = KeyParamsData
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { RegistrationData } from './RegistrationData'
|
||||
|
||||
export type RegistrationResponse = HttpResponse & {
|
||||
data: RegistrationData
|
||||
}
|
||||
export type RegistrationResponse = RegistrationData
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { SessionListEntry } from './SessionListEntry'
|
||||
|
||||
export type SessionListResponse = HttpResponse & { data: SessionListEntry[] }
|
||||
export type SessionListResponse = SessionListEntry[]
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { SessionRenewalData } from './SessionRenewalData'
|
||||
|
||||
export type SessionRenewalResponse = HttpResponse & {
|
||||
data: SessionRenewalData
|
||||
}
|
||||
export type SessionRenewalResponse = SessionRenewalData
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { SignInData } from './SignInData'
|
||||
|
||||
export type SignInResponse = HttpResponse & {
|
||||
data: SignInData
|
||||
}
|
||||
export type SignInResponse = SignInData
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { HttpSuccessResponse } from '../Http/HttpResponse'
|
||||
|
||||
export type SignOutResponse = HttpResponse & Record<string, unknown>
|
||||
export type SignOutResponse = HttpSuccessResponse
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { Error } from '../Http/Error'
|
||||
|
||||
export class ClientDisplayableError {
|
||||
constructor(public text: string, public title?: string, public tag?: string) {
|
||||
console.error('Client Displayable Error:', text, title || '', tag || '')
|
||||
}
|
||||
|
||||
static FromError(error: Error) {
|
||||
static FromError(error: { message: string; tag?: string }) {
|
||||
return new ClientDisplayableError(error.message, undefined, error.tag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type CloseUploadSessionResponse = MinimalHttpResponse & {
|
||||
success: boolean
|
||||
message: string
|
||||
}
|
||||
export type CloseUploadSessionResponse = { success: boolean; message: string }
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { CreateValetTokenResponseData } from './CreateValetTokenResponseData'
|
||||
|
||||
export type CreateValetTokenResponse = MinimalHttpResponse & {
|
||||
data: CreateValetTokenResponseData
|
||||
}
|
||||
export type CreateValetTokenResponse = CreateValetTokenResponseData
|
||||
|
||||
@@ -1,5 +1 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type DownloadFileChunkResponse = MinimalHttpResponse & {
|
||||
data: ArrayBuffer
|
||||
}
|
||||
export type DownloadFileChunkResponse = ArrayBuffer
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type StartUploadSessionResponse = MinimalHttpResponse & {
|
||||
export type StartUploadSessionResponse = {
|
||||
success: boolean
|
||||
uploadId: string
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type UploadFileChunkResponse = MinimalHttpResponse & {
|
||||
export type UploadFileChunkResponse = {
|
||||
success: boolean
|
||||
}
|
||||
|
||||
5
packages/responses/src/Domain/Http/DeprecatedError.ts
Normal file
5
packages/responses/src/Domain/Http/DeprecatedError.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export type DeprecatedError = {
|
||||
message: string
|
||||
status: number
|
||||
tag?: string
|
||||
}
|
||||
13
packages/responses/src/Domain/Http/DeprecatedHttpResponse.ts
Normal file
13
packages/responses/src/Domain/Http/DeprecatedHttpResponse.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { DeprecatedError } from './DeprecatedError'
|
||||
import { HttpStatusCode } from './HttpStatusCode'
|
||||
import { DeprecatedResponseMeta } from './DeprecatedResponseMeta'
|
||||
|
||||
export type DeprecatedHttpResponse = {
|
||||
status: HttpStatusCode
|
||||
error?: DeprecatedError
|
||||
data?: {
|
||||
error?: DeprecatedError
|
||||
}
|
||||
meta?: DeprecatedResponseMeta
|
||||
headers?: Map<string, string | null>
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { DeprecatedError } from './DeprecatedError'
|
||||
import { HttpStatusCode } from './HttpStatusCode'
|
||||
|
||||
export type DeprecatedMinimalHttpResponse = {
|
||||
status: HttpStatusCode
|
||||
error?: DeprecatedError
|
||||
headers?: Map<string, string | null>
|
||||
}
|
||||
11
packages/responses/src/Domain/Http/DeprecatedResponseMeta.ts
Normal file
11
packages/responses/src/Domain/Http/DeprecatedResponseMeta.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Role } from '@standardnotes/security'
|
||||
|
||||
export type DeprecatedResponseMeta = {
|
||||
auth: {
|
||||
userUuid?: string
|
||||
roles?: Role[]
|
||||
}
|
||||
server: {
|
||||
filesServerUrl?: string
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
export enum StatusCode {
|
||||
export enum DeprecatedStatusCode {
|
||||
LocalValidationError = 10,
|
||||
CanceledMfa = 11,
|
||||
UnknownError = 12,
|
||||
|
||||
HttpStatusMinSuccess = 200,
|
||||
HttpStatusNoContent = 204,
|
||||
@@ -1,10 +0,0 @@
|
||||
export type Error = {
|
||||
message: string
|
||||
status: number
|
||||
tag?: string
|
||||
/** In the case of MFA required responses,
|
||||
* the required prompt is returned as part of the error */
|
||||
payload?: {
|
||||
mfa_key?: string
|
||||
}
|
||||
}
|
||||
15
packages/responses/src/Domain/Http/ErrorTag.ts
Normal file
15
packages/responses/src/Domain/Http/ErrorTag.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export enum ErrorTag {
|
||||
MfaInvalid = 'mfa-invalid',
|
||||
MfaRequired = 'mfa-required',
|
||||
U2FRequired = 'u2f-required',
|
||||
RefreshTokenInvalid = 'invalid-refresh-token',
|
||||
RefreshTokenExpired = 'expired-refresh-token',
|
||||
AccessTokenExpired = 'expired-access-token',
|
||||
ParametersInvalid = 'invalid-parameters',
|
||||
RevokedSession = 'revoked-session',
|
||||
AuthInvalid = 'invalid-auth',
|
||||
ReadOnlyAccess = 'read-only-access',
|
||||
|
||||
ClientValidationError = 'client-validation-error',
|
||||
ClientCanceledMfa = 'client-canceled-mfa',
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
import { HttpResponse } from './HttpResponse'
|
||||
import { StatusCode } from './StatusCode'
|
||||
|
||||
export function isErrorResponseExpiredToken(errorResponse: HttpResponse): boolean {
|
||||
return errorResponse.status === StatusCode.HttpStatusExpiredAccessToken
|
||||
}
|
||||
6
packages/responses/src/Domain/Http/HttpError.ts
Normal file
6
packages/responses/src/Domain/Http/HttpError.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ErrorTag } from './ErrorTag'
|
||||
|
||||
export type HttpError = {
|
||||
message: string
|
||||
tag?: ErrorTag
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { HttpError } from './HttpError'
|
||||
|
||||
export type HttpErrorResponseBody = {
|
||||
error: HttpError
|
||||
}
|
||||
1
packages/responses/src/Domain/Http/HttpHeaders.ts
Normal file
1
packages/responses/src/Domain/Http/HttpHeaders.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type HttpHeaders = Map<string, string | null>
|
||||
13
packages/responses/src/Domain/Http/HttpRequest.ts
Normal file
13
packages/responses/src/Domain/Http/HttpRequest.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { HttpRequestParams } from './HttpRequestParams'
|
||||
import { HttpVerb } from './HttpVerb'
|
||||
|
||||
export type HttpRequest = {
|
||||
url: string
|
||||
params?: HttpRequestParams
|
||||
rawBytes?: Uint8Array
|
||||
verb: HttpVerb
|
||||
authentication?: string
|
||||
customHeaders?: Record<string, string>[]
|
||||
responseType?: XMLHttpRequestResponseType
|
||||
external?: boolean
|
||||
}
|
||||
1
packages/responses/src/Domain/Http/HttpRequestParams.ts
Normal file
1
packages/responses/src/Domain/Http/HttpRequestParams.ts
Normal file
@@ -0,0 +1 @@
|
||||
export type HttpRequestParams = unknown
|
||||
@@ -1,13 +1,26 @@
|
||||
import { StatusCode } from './StatusCode'
|
||||
import { Error } from './Error'
|
||||
import { ResponseMeta } from './ResponseMeta'
|
||||
import { HttpErrorResponseBody } from './HttpErrorResponseBody'
|
||||
import { HttpResponseMeta } from './HttpResponseMeta'
|
||||
import { HttpHeaders } from './HttpHeaders'
|
||||
import { HttpStatusCode } from './HttpStatusCode'
|
||||
|
||||
export type HttpResponse = {
|
||||
status?: StatusCode
|
||||
error?: Error
|
||||
data?: {
|
||||
error?: Error
|
||||
}
|
||||
meta?: ResponseMeta
|
||||
headers?: Map<string, string | null>
|
||||
type AnySuccessRecord = Record<string, unknown> & { error?: never }
|
||||
|
||||
interface HttpResponseBase {
|
||||
status: HttpStatusCode
|
||||
meta?: HttpResponseMeta
|
||||
headers?: HttpHeaders
|
||||
}
|
||||
|
||||
export interface HttpErrorResponse extends HttpResponseBase {
|
||||
data: HttpErrorResponseBody
|
||||
}
|
||||
|
||||
export interface HttpSuccessResponse<T = AnySuccessRecord> extends HttpResponseBase {
|
||||
data: T
|
||||
}
|
||||
|
||||
export type HttpResponse<T = AnySuccessRecord> = HttpErrorResponse | HttpSuccessResponse<T>
|
||||
|
||||
export function isErrorResponse<T>(response: HttpResponse<T>): response is HttpErrorResponse {
|
||||
return (response.data as HttpErrorResponseBody)?.error != undefined
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Role } from '@standardnotes/security'
|
||||
|
||||
export type ResponseMeta = {
|
||||
export type HttpResponseMeta = {
|
||||
auth: {
|
||||
userUuid?: string
|
||||
roles?: Role[]
|
||||
11
packages/responses/src/Domain/Http/HttpStatusCode.ts
Normal file
11
packages/responses/src/Domain/Http/HttpStatusCode.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export enum HttpStatusCode {
|
||||
Success = 200,
|
||||
NoContent = 204,
|
||||
MultipleChoices = 300,
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
Forbidden = 403,
|
||||
Gone = 410,
|
||||
ExpiredAccessToken = 498,
|
||||
InternalServerError = 500,
|
||||
}
|
||||
7
packages/responses/src/Domain/Http/HttpVerb.ts
Normal file
7
packages/responses/src/Domain/Http/HttpVerb.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export enum HttpVerb {
|
||||
Get = 'GET',
|
||||
Post = 'POST',
|
||||
Put = 'PUT',
|
||||
Patch = 'PATCH',
|
||||
Delete = 'DELETE',
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { StatusCode } from './StatusCode'
|
||||
import { Error } from './Error'
|
||||
|
||||
export type MinimalHttpResponse = {
|
||||
status?: StatusCode
|
||||
error?: Error
|
||||
headers?: Map<string, string | null>
|
||||
}
|
||||
15
packages/responses/src/Domain/Http/index.ts
Normal file
15
packages/responses/src/Domain/Http/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export * from './ErrorTag'
|
||||
export * from './HttpErrorResponseBody'
|
||||
export * from './HttpHeaders'
|
||||
export * from './HttpRequest'
|
||||
export * from './HttpRequestParams'
|
||||
export * from './HttpResponse'
|
||||
export * from './HttpResponseMeta'
|
||||
export * from './HttpVerb'
|
||||
export * from './DeprecatedError'
|
||||
export * from './DeprecatedHttpResponse'
|
||||
export * from './DeprecatedMinimalHttpResponses'
|
||||
export * from './DeprecatedResponseMeta'
|
||||
export * from './DeprecatedStatusCode'
|
||||
export * from './HttpStatusCode'
|
||||
export * from './HttpError'
|
||||
@@ -1,8 +1,5 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { IntegrityPayload } from './IntegrityPayload'
|
||||
|
||||
export type CheckIntegrityResponse = MinimalHttpResponse & {
|
||||
data: {
|
||||
mismatches: IntegrityPayload[]
|
||||
}
|
||||
export type CheckIntegrityResponse = {
|
||||
mismatches: IntegrityPayload[]
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { ServerItemResponse } from './ServerItemResponse'
|
||||
|
||||
export type GetSingleItemResponse = MinimalHttpResponse & {
|
||||
data:
|
||||
| {
|
||||
success: true
|
||||
item: ServerItemResponse
|
||||
}
|
||||
| {
|
||||
success: false
|
||||
message: string
|
||||
}
|
||||
}
|
||||
export type GetSingleItemResponse =
|
||||
| {
|
||||
success: true
|
||||
item: ServerItemResponse
|
||||
}
|
||||
| {
|
||||
success: false
|
||||
message: string
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { RawSyncData } from './RawSyncData'
|
||||
|
||||
export type RawSyncResponse = HttpResponse & { data: RawSyncData }
|
||||
export type RawSyncResponse = RawSyncData
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { AnyKeyParamsContent, ContentType } from '@standardnotes/common'
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { DeprecatedHttpResponse } from '../Http/DeprecatedHttpResponse'
|
||||
import { ServerItemResponse } from '../Item/ServerItemResponse'
|
||||
|
||||
export type ActionResponse = HttpResponse & {
|
||||
export type ActionResponse = DeprecatedHttpResponse & {
|
||||
description: string
|
||||
supported_types: ContentType[]
|
||||
deprecation?: string
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { ListedAccountInfo } from './ListedAccountInfo'
|
||||
|
||||
export type ListedAccountInfoResponse = HttpResponse & {
|
||||
data: ListedAccountInfo
|
||||
}
|
||||
export type ListedAccountInfoResponse = ListedAccountInfo
|
||||
|
||||
@@ -1,5 +1 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type ListedRegistrationResponse = MinimalHttpResponse & {
|
||||
data?: unknown
|
||||
}
|
||||
export type ListedRegistrationResponse = unknown
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
|
||||
export type DeleteSettingResponse = MinimalHttpResponse
|
||||
export type DeleteSettingResponse = HttpResponse
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { AvailableSubscriptions } from './AvailableSubscriptions'
|
||||
|
||||
export type GetAvailableSubscriptionsResponse = MinimalHttpResponse & {
|
||||
data?: AvailableSubscriptions
|
||||
}
|
||||
export type GetAvailableSubscriptionsResponse = AvailableSubscriptions
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { FeatureDescription } from '@standardnotes/features'
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type GetOfflineFeaturesResponse = MinimalHttpResponse & {
|
||||
data?: {
|
||||
features: FeatureDescription[]
|
||||
roles: string[]
|
||||
}
|
||||
export type GetOfflineFeaturesResponse = {
|
||||
features: FeatureDescription[]
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { SettingData } from './SettingData'
|
||||
|
||||
export type GetSettingResponse = MinimalHttpResponse & {
|
||||
data?: {
|
||||
success?: boolean
|
||||
setting?: SettingData
|
||||
}
|
||||
export type GetSettingResponse = {
|
||||
success?: boolean
|
||||
setting?: SettingData
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { Subscription } from '@standardnotes/security'
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type GetSubscriptionResponse = MinimalHttpResponse & {
|
||||
data?: {
|
||||
subscription?: Subscription
|
||||
}
|
||||
export type GetSubscriptionResponse = {
|
||||
subscription?: Subscription
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
import { SettingData } from './SettingData'
|
||||
|
||||
export type ListSettingsResponse = MinimalHttpResponse & {
|
||||
data?: {
|
||||
settings?: SettingData[]
|
||||
}
|
||||
export type ListSettingsResponse = {
|
||||
settings?: SettingData[]
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type PostSubscriptionTokensResponse = MinimalHttpResponse & {
|
||||
data?: {
|
||||
token: string
|
||||
}
|
||||
export type PostSubscriptionTokensResponse = {
|
||||
token: string
|
||||
}
|
||||
|
||||
@@ -1,3 +1 @@
|
||||
import { MinimalHttpResponse } from '../Http/MinimalHttpResponses'
|
||||
|
||||
export type UpdateSettingResponse = MinimalHttpResponse
|
||||
export type UpdateSettingResponse = unknown
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
import { HttpResponse } from '../Http/HttpResponse'
|
||||
import { UserFeaturesData } from './UserFeaturesData'
|
||||
|
||||
export type UserFeaturesResponse = HttpResponse & {
|
||||
data: UserFeaturesData
|
||||
}
|
||||
export type UserFeaturesResponse = UserFeaturesData
|
||||
|
||||
@@ -21,12 +21,7 @@ export * from './Files/CreateValetTokenResponseData'
|
||||
export * from './Files/DownloadFileChunkResponse'
|
||||
export * from './Files/StartUploadSessionResponse'
|
||||
export * from './Files/UploadFileChunkResponse'
|
||||
export * from './Http/Error'
|
||||
export * from './Http/Functions'
|
||||
export * from './Http/HttpResponse'
|
||||
export * from './Http/MinimalHttpResponses'
|
||||
export * from './Http/ResponseMeta'
|
||||
export * from './Http/StatusCode'
|
||||
export * from './Http'
|
||||
export * from './Item/ApiEndpointParam'
|
||||
export * from './Item/CheckIntegrityResponse'
|
||||
export * from './Item/ConflictParams'
|
||||
|
||||
Reference in New Issue
Block a user