This repository has been archived on 2024-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
frontend/src/services/abstractService.ts

510 lines
13 KiB
TypeScript

import {AuthenticatedHTTPFactory} from '@/helpers/fetcher'
import type {Method} from 'axios'
import {objectToSnakeCase} from '@/helpers/case'
import AbstractModel from '@/models/abstractModel'
import type {IAbstract} from '@/modelTypes/IAbstract'
import type {Right} from '@/constants/rights'
interface PathsAll {
create : string
get : string
getAll : string
update : string
delete : string
reset?: string
}
type PathsGeneric = Partial<PathsAll>;
function convertObject(o: Record<string, unknown>) {
if (o instanceof Date) {
return o.toISOString()
}
return o
}
function prepareParams(params: Record<string, unknown | unknown[]>) {
if (typeof params !== 'object') {
return params
}
for (const p in params) {
if (Array.isArray(params[p])) {
params[p] = params[p].map(convertObject)
continue
}
params[p] = convertObject(params[p])
}
return objectToSnakeCase(params)
}
type VarsFromPathTemplate<P extends string> = P extends `${string}{${infer FirstKey}}${infer Rest}`
? [FirstKey, ...VarsFromPathTemplate<Rest>]
: [];
/**
* @example
* type T = ReplacerDictFromPath<'/teams/{teamId}/members/{username}'>;
* const dict: T = {
* teamId: 1,
* username: 'demo',
* }
*/
type ReplacerDictFromPath<P extends string> = Record<
VarsFromPathTemplate<P>[number],
string | number
>
type ReplacerDictOrNeverFromPath<P extends string | undefined | null> =
P extends string
? ReplacerDictFromPath<P>
: never;
/**
* Prohibit the use of `string` as values, i.e. require a more concrete type:
* string literal or template literal.
*/
type EnsureConst<T extends Record<string, string>> = {
[P in keyof T]: string extends T[P] ? never : T[P];
};
export default abstract class AbstractService<
Model extends IAbstract = IAbstract,
// Not sure if the default value makes sense here. Put it only because a non-optional
// param can't go after an optional one.
P extends PathsGeneric = EnsureConst<PathsGeneric>,
> {
/////////////////////////////
// Initial variable definitions
///////////////////////////
http
loading = false
uploadProgress = 0
paths: P
// This contains the total number of pages and the number of results for the current page
totalPages = 0
resultCount = 0
/////////////
// Service init
///////////
/**
* The abstract constructor.
* @param [paths] An object with all paths.
*/
constructor(paths : EnsureConst<P> = ({} as const)) {
this.http = AuthenticatedHTTPFactory()
// Set the interceptors to process every request
this.http.interceptors.request.use((config) => {
switch (config.method) {
case 'post':
if (this.useUpdateInterceptor()) {
config.data = this.beforeUpdate(config.data)
config.data = objectToSnakeCase(config.data)
}
break
case 'put':
if (this.useCreateInterceptor()) {
config.data = this.beforeCreate(config.data)
config.data = objectToSnakeCase(config.data)
}
break
case 'delete':
if (this.useDeleteInterceptor()) {
config.data = this.beforeDelete(config.data)
config.data = objectToSnakeCase(config.data)
}
break
}
return config
})
this.paths = { ...paths } as P
}
/**
* Whether or not to use the create interceptor which processes a request payload into json
*/
useCreateInterceptor(): boolean {
return true
}
/**
* Whether or not to use the update interceptor which processes a request payload into json
*/
useUpdateInterceptor(): boolean {
return true
}
/**
* Whether or not to use the delete interceptor which processes a request payload into json
*/
useDeleteInterceptor(): boolean {
return true
}
/////////////////
// Helper functions
///////////////
/**
* Returns an object with all route parameters and their values.
* @example
* getRouteReplacements(
* '/tasks/{taskId}/assignees/{userId}',
* { taskId: 7, userId: 2 },
* )
* // { "{taskId}": 7, "{userId}": 2 }
*/
getRouteReplacements(route : string, parameters : Record<string, unknown> = {}) {
const replace$$1: Record<string, unknown> = {}
let pattern = this.getRouteParameterPattern()
pattern = new RegExp(pattern instanceof RegExp ? pattern.source : pattern, 'g')
for (let parameter; (parameter = pattern.exec(route)) !== null;) {
replace$$1[parameter[0]] = parameters[parameter[1]]
}
return replace$$1
}
/**
* Holds the replacement pattern for url paths, can be overwritten by implementations.
*/
getRouteParameterPattern(): RegExp {
return /{([^}]+)}/
}
/**
* Returns a fully-ready-ready-to-make-a-request-to route with replaced parameters.
* @example
* getReplacedRoute('/projects/{projectId}/tasks', { projectId: 3 })
* === '/projects/{projectId}/tasks'
*/
getReplacedRoute(path : string, pathparams : Record<string, unknown>) : string {
const replacements = this.getRouteReplacements(path, pathparams)
return Object.entries(replacements).reduce(
(result, [parameter, value]) => result.replace(parameter, value as string),
path,
)
}
/**
* setLoading is a method which sets the loading variable to true, after a timeout of 100ms.
* It has the timeout to prevent the loading indicator from showing for only a blink of an eye in the
* case the api returns a response in < 100ms.
* But because the timeout is created using setTimeout, it will still trigger even if the request is
* already finished, so we return a method to call in that case.
*/
setLoading() {
const timeout = setTimeout(() => {
this.loading = true
}, 100)
return () => {
clearTimeout(timeout)
this.loading = false
}
}
//////////////////
// Default factories
// It is possible to specify a factory for each type of request.
// This makes it possible to have different models returned from different routes.
// Specific factories for each request are completly optional, if these are not specified, the defautl factory is used.
////////////////
/**
* The modelFactory returns a model from an object.
* This one here is the default one, usually the service definitions for a model will override this.
*/
modelFactory(data : Partial<Model>) {
return data as Model
}
/**
* This is the model factory for get requests.
*/
modelGetFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for get all requests.
*/
modelGetAllFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for create requests.
*/
modelCreateFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for update requests.
*/
modelUpdateFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
//////////////
// Preprocessors
////////////
/**
* Default preprocessor for get requests
*/
beforeGet(model : Model) {
return model
}
/**
* Default preprocessor for create requests
*/
beforeCreate(model : Model) {
return model
}
/**
* Default preprocessor for update requests
*/
beforeUpdate(model : Model) {
return model
}
/**
* Default preprocessor for delete requests
*/
beforeDelete(model : Model) {
return model
}
///////////////
// Global actions
/////////////
/**
* Performs a get request to the url specified before.
* @param model The model to use. The request path is built using the values from the model.
* @param params Optional query parameters
*/
// TODO refactor: if `paths.get` is undefined, it's better if `get()` method
// is undefined as well, instead of just returning `never`.
get<T extends P['get']>(
model : ReplacerDictOrNeverFromPath<T>,
params = {},
): Promise<T extends string ? Model : never> {
if (!this.paths.get) {
throw new Error('This model is not able to get data.')
}
return this.getM(this.paths.get, model, params)
}
/**
* This is a more abstract implementation which only does a get request.
* Services which need more flexibility can use this.
*/
async getM<T extends string>(
url : T,
model : ReplacerDictFromPath<T>,
params: Record<string, unknown> = {},
) {
const cancel = this.setLoading()
model = this.beforeGet(model)
const finalUrl = this.getReplacedRoute(url, model)
try {
const response = await this.http.get(finalUrl, {params: prepareParams(params)})
const result = this.modelGetFactory(response.data)
result.maxRight = Number(response.headers['x-max-right']) as Right
return result
} finally {
cancel()
}
}
async getBlobUrl(url : string, method : Method = 'GET', data = {}) {
const response = await this.http({
url,
method,
responseType: 'blob',
data,
})
return window.URL.createObjectURL(new Blob([response.data]))
}
/**
* Performs a get request to the url specified before.
* The difference between this and get() is this one is used to get a bunch of data (an array), not just a single object.
* @param model The model to use. The request path is built using the values from the model.
* @param params Optional query parameters
* @param page The page to get
*/
// TODO refactor: ensure that if `ReplacerDict` is an empty object type then you
// can't pass an arbitrary object literal as `model`.
async getAll<T extends P['getAll']>(
model : ReplacerDictOrNeverFromPath<T> = new AbstractModel({}),
params = {},
page = 1,
): Promise<T extends string ? Model[] : never> {
if (!this.paths.getAll) {
throw new Error('This model is not able to get data.')
}
params.page = page
const cancel = this.setLoading()
model = this.beforeGet(model)
const finalUrl = this.getReplacedRoute(this.paths.getAll, model)
try {
const response = await this.http.get(finalUrl, {params: prepareParams(params)})
this.resultCount = Number(response.headers['x-pagination-result-count'])
this.totalPages = Number(response.headers['x-pagination-total-pages'])
if (response.data === null) {
return []
}
return response.data.map(entry => this.modelGetAllFactory(entry))
} finally {
cancel()
}
}
/**
* Performs a put request to the url specified before
* @returns {Promise<any | never>}
*/
async create<T extends P['create']>(
model : Model & ReplacerDictOrNeverFromPath<T>,
): Promise<T extends string ? Model : never> {
if (!this.paths.create) {
throw new Error('This model is not able to create data.')
}
const cancel = this.setLoading()
const finalUrl = this.getReplacedRoute(this.paths.create, model)
try {
const response = await this.http.put(finalUrl, model)
const result = this.modelCreateFactory(response.data)
if (typeof model.maxRight !== 'undefined') {
result.maxRight = model.maxRight
}
return result
} finally {
cancel()
}
}
/**
* An abstract implementation to send post requests.
* Services can use this to implement functions to do post requests other than using the update method.
*/
async post(url : string, model : Model) {
const cancel = this.setLoading()
try {
const response = await this.http.post(url, model)
const result = this.modelUpdateFactory(response.data)
if (typeof model.maxRight !== 'undefined') {
result.maxRight = model.maxRight
}
return result
} finally {
cancel()
}
}
/**
* Performs a post request to the update url
*/
update<T extends P['update']>(
model : Model & ReplacerDictOrNeverFromPath<T>,
): Promise<T extends string ? Model : never> {
if (!this.paths.update) {
throw new Error('This model is not able to update data.')
}
const finalUrl = this.getReplacedRoute(this.paths.update, model)
return this.post(finalUrl, model)
}
/**
* Performs a delete request to the update url
*/
async delete<T extends P['delete']>(
model : ReplacerDictOrNeverFromPath<T>,
) {
if (!this.paths.delete) {
throw new Error('This model is not able to delete data.')
}
const cancel = this.setLoading()
const finalUrl = this.getReplacedRoute(this.paths.delete, model)
try {
const {data} = await this.http.delete(finalUrl, model)
return data
} finally {
cancel()
}
}
/**
* Uploads a file to a url.
* @param url
* @param file {IFile}
* @param fieldName The name of the field the file is uploaded to.
*/
uploadFile(url : string, file: File, fieldName : string) {
return this.uploadBlob(url, new Blob([file]), fieldName, file.name)
}
/**
* Uploads a blob to a url.
*/
uploadBlob(url : string, blob: Blob, fieldName: string, filename : string) {
const data = new FormData()
data.append(fieldName, blob, filename)
return this.uploadFormData(url, data)
}
/**
* Uploads a form data object.
*/
async uploadFormData(url : string, formData: FormData) {
const cancel = this.setLoading()
try {
const response = await this.http.put(
url,
formData,
{
headers: {
'Content-Type':
'multipart/form-data; boundary=' + formData._boundary,
},
// fix upload issue after upgrading to axios to 1.0.0
// see: https://github.com/axios/axios/issues/4885#issuecomment-1222419132
transformRequest: formData => formData,
onUploadProgress: ({progress}) => {
this.uploadProgress = progress? Math.round((progress * 100)) : 0
},
},
)
return this.modelCreateFactory(response.data)
} finally {
this.uploadProgress = 0
cancel()
}
}
}