feat: improve conversion

This commit is contained in:
Dominik Pschenitschni 2022-02-14 00:28:48 +01:00
parent c70cbf4fb4
commit fa82929a69
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
1 changed files with 22 additions and 21 deletions

View File

@ -11,7 +11,7 @@ interface Paths {
delete : string
}
function convertObject(o) {
function convertObject(o: Record<string, unknown>) {
if (o instanceof Date) {
return o.toISOString()
}
@ -19,7 +19,7 @@ function convertObject(o) {
return o
}
function prepareParams(params) {
function prepareParams(params: Record<string, unknown | any[]>) {
if (typeof params !== 'object') {
return params
}
@ -36,7 +36,7 @@ function prepareParams(params) {
return objectToSnakeCase(params)
}
export default class AbstractService {
export default class AbstractService<Model extends AbstractModel> {
/////////////////////////////
// Initial variable definitions
@ -116,6 +116,7 @@ export default class AbstractService {
* Whether or not to use the update interceptor which processes a request payload into json
*/
useUpdateInterceptor(): boolean {
return true
}
/**
@ -133,7 +134,7 @@ export default class AbstractService {
* Returns an object with all route parameters and their values.
*/
getRouteReplacements(route : string, parameters = {}) {
const replace$$1 = {}
const replace$$1: {} = {}
let pattern = this.getRouteParameterPattern()
pattern = new RegExp(pattern instanceof RegExp ? pattern.source : pattern, 'g')
@ -190,35 +191,35 @@ export default class AbstractService {
* The modelFactory returns an model from an object.
* This one here is the default one, usually the service definitions for a model will override this.
*/
modelFactory(data : Partial<AbstractModel>) {
modelFactory(data : Partial<Model>) {
return new AbstractModel(data)
}
/**
* This is the model factory for get requests.
*/
modelGetFactory(data : Partial<AbstractModel>) {
modelGetFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for get all requests.
*/
modelGetAllFactory(data : Partial<AbstractModel>) {
modelGetAllFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for create requests.
*/
modelCreateFactory(data : Partial<AbstractModel>) {
modelCreateFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
/**
* This is the model factory for update requests.
*/
modelUpdateFactory(data : Partial<AbstractModel>) {
modelUpdateFactory(data : Partial<Model>) {
return this.modelFactory(data)
}
@ -229,28 +230,28 @@ export default class AbstractService {
/**
* Default preprocessor for get requests
*/
beforeGet(model : InstanceType<typeof AbstractModel>) {
beforeGet(model : Model) {
return model
}
/**
* Default preprocessor for create requests
*/
beforeCreate(model : InstanceType<typeof AbstractModel>) {
beforeCreate(model : Model) {
return model
}
/**
* Default preprocessor for update requests
*/
beforeUpdate(model : InstanceType<typeof AbstractModel>) {
beforeUpdate(model : Model) {
return model
}
/**
* Default preprocessor for delete requests
*/
beforeDelete(model : InstanceType<typeof AbstractModel>) {
beforeDelete(model : Model) {
return model
}
@ -263,7 +264,7 @@ export default class AbstractService {
* @param model The model to use. The request path is built using the values from the model.
* @param params Optional query parameters
*/
get(model : InstanceType<typeof AbstractModel>, params = {}) {
get(model : Model, params = {}) {
if (this.paths.get === '') {
throw new Error('This model is not able to get data.')
}
@ -308,7 +309,7 @@ export default class AbstractService {
* @param params Optional query parameters
* @param page The page to get
*/
async getAll(model : InstanceType<typeof AbstractModel> = new AbstractModel({}), params = {}, page = 1) {
async getAll(model : Model = new AbstractModel({}), params = {}, page = 1) {
if (this.paths.getAll === '') {
throw new Error('This model is not able to get data.')
}
@ -341,7 +342,7 @@ export default class AbstractService {
* Performs a put request to the url specified before
* @returns {Promise<any | never>}
*/
async create(model : InstanceType<typeof AbstractModel>) {
async create(model : Model) {
if (this.paths.create === '') {
throw new Error('This model is not able to create data.')
}
@ -365,7 +366,7 @@ export default class AbstractService {
* 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 : InstanceType<typeof AbstractModel>) {
async post(url : string, model : Model) {
const cancel = this.setLoading()
try {
@ -383,7 +384,7 @@ export default class AbstractService {
/**
* Performs a post request to the update url
*/
update(model : InstanceType<typeof AbstractModel>) {
update(model : Model) {
if (this.paths.update === '') {
throw new Error('This model is not able to update data.')
}
@ -395,7 +396,7 @@ export default class AbstractService {
/**
* Performs a delete request to the update url
*/
async delete(model : InstanceType<typeof AbstractModel>) {
async delete(model : Model) {
if (this.paths.delete === '') {
throw new Error('This model is not able to delete data.')
}
@ -424,7 +425,7 @@ export default class AbstractService {
/**
* Uploads a blob to a url.
*/
uploadBlob(url : string, blob, fieldName, filename : string) {
uploadBlob(url : string, blob: Blob, fieldName: string, filename : string) {
const data = new FormData()
data.append(fieldName, blob, filename)
return this.uploadFormData(url, data)
@ -433,7 +434,7 @@ export default class AbstractService {
/**
* Uploads a form data object.
*/
async uploadFormData(url : string, formData) {
async uploadFormData(url : string, formData: Record<string, unknown>) {
const cancel = this.setLoading()
try {
const response = await this.http.put(