feat: improve conversion
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Dominik Pschenitschni 2022-02-14 00:28:48 +01:00
parent e953324dff
commit 6c64e16469
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 delete : string
} }
function convertObject(o) { function convertObject(o: Record<string, unknown>) {
if (o instanceof Date) { if (o instanceof Date) {
return o.toISOString() return o.toISOString()
} }
@ -19,7 +19,7 @@ function convertObject(o) {
return o return o
} }
function prepareParams(params) { function prepareParams(params: Record<string, unknown | any[]>) {
if (typeof params !== 'object') { if (typeof params !== 'object') {
return params return params
} }
@ -36,7 +36,7 @@ function prepareParams(params) {
return objectToSnakeCase(params) return objectToSnakeCase(params)
} }
export default class AbstractService { export default class AbstractService<Model extends AbstractModel> {
///////////////////////////// /////////////////////////////
// Initial variable definitions // 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 * Whether or not to use the update interceptor which processes a request payload into json
*/ */
useUpdateInterceptor(): boolean { useUpdateInterceptor(): boolean {
return true
} }
/** /**
@ -133,7 +134,7 @@ export default class AbstractService {
* Returns an object with all route parameters and their values. * Returns an object with all route parameters and their values.
*/ */
getRouteReplacements(route : string, parameters = {}) { getRouteReplacements(route : string, parameters = {}) {
const replace$$1 = {} const replace$$1: {} = {}
let pattern = this.getRouteParameterPattern() let pattern = this.getRouteParameterPattern()
pattern = new RegExp(pattern instanceof RegExp ? pattern.source : pattern, 'g') 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. * 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. * 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) return new AbstractModel(data)
} }
/** /**
* This is the model factory for get requests. * This is the model factory for get requests.
*/ */
modelGetFactory(data : Partial<AbstractModel>) { modelGetFactory(data : Partial<Model>) {
return this.modelFactory(data) return this.modelFactory(data)
} }
/** /**
* This is the model factory for get all requests. * This is the model factory for get all requests.
*/ */
modelGetAllFactory(data : Partial<AbstractModel>) { modelGetAllFactory(data : Partial<Model>) {
return this.modelFactory(data) return this.modelFactory(data)
} }
/** /**
* This is the model factory for create requests. * This is the model factory for create requests.
*/ */
modelCreateFactory(data : Partial<AbstractModel>) { modelCreateFactory(data : Partial<Model>) {
return this.modelFactory(data) return this.modelFactory(data)
} }
/** /**
* This is the model factory for update requests. * This is the model factory for update requests.
*/ */
modelUpdateFactory(data : Partial<AbstractModel>) { modelUpdateFactory(data : Partial<Model>) {
return this.modelFactory(data) return this.modelFactory(data)
} }
@ -229,28 +230,28 @@ export default class AbstractService {
/** /**
* Default preprocessor for get requests * Default preprocessor for get requests
*/ */
beforeGet(model : InstanceType<typeof AbstractModel>) { beforeGet(model : Model) {
return model return model
} }
/** /**
* Default preprocessor for create requests * Default preprocessor for create requests
*/ */
beforeCreate(model : InstanceType<typeof AbstractModel>) { beforeCreate(model : Model) {
return model return model
} }
/** /**
* Default preprocessor for update requests * Default preprocessor for update requests
*/ */
beforeUpdate(model : InstanceType<typeof AbstractModel>) { beforeUpdate(model : Model) {
return model return model
} }
/** /**
* Default preprocessor for delete requests * Default preprocessor for delete requests
*/ */
beforeDelete(model : InstanceType<typeof AbstractModel>) { beforeDelete(model : Model) {
return 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 model The model to use. The request path is built using the values from the model.
* @param params Optional query parameters * @param params Optional query parameters
*/ */
get(model : InstanceType<typeof AbstractModel>, params = {}) { get(model : Model, params = {}) {
if (this.paths.get === '') { if (this.paths.get === '') {
throw new Error('This model is not able to get data.') 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 params Optional query parameters
* @param page The page to get * @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 === '') { if (this.paths.getAll === '') {
throw new Error('This model is not able to get data.') 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 * Performs a put request to the url specified before
* @returns {Promise<any | never>} * @returns {Promise<any | never>}
*/ */
async create(model : InstanceType<typeof AbstractModel>) { async create(model : Model) {
if (this.paths.create === '') { if (this.paths.create === '') {
throw new Error('This model is not able to create data.') 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. * An abstract implementation to send post requests.
* Services can use this to implement functions to do post requests other than using the update method. * 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() const cancel = this.setLoading()
try { try {
@ -383,7 +384,7 @@ export default class AbstractService {
/** /**
* Performs a post request to the update url * Performs a post request to the update url
*/ */
update(model : InstanceType<typeof AbstractModel>) { update(model : Model) {
if (this.paths.update === '') { if (this.paths.update === '') {
throw new Error('This model is not able to update data.') 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 * Performs a delete request to the update url
*/ */
async delete(model : InstanceType<typeof AbstractModel>) { async delete(model : Model) {
if (this.paths.delete === '') { if (this.paths.delete === '') {
throw new Error('This model is not able to delete data.') 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. * 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() const data = new FormData()
data.append(fieldName, blob, filename) data.append(fieldName, blob, filename)
return this.uploadFormData(url, data) return this.uploadFormData(url, data)
@ -433,7 +434,7 @@ export default class AbstractService {
/** /**
* Uploads a form data object. * Uploads a form data object.
*/ */
async uploadFormData(url : string, formData) { async uploadFormData(url : string, formData: Record<string, unknown>) {
const cancel = this.setLoading() const cancel = this.setLoading()
try { try {
const response = await this.http.put( const response = await this.http.put(