Skip to main content

Service class

What Service class is

Service class is a JavaScript class that implements an arbitrary piece of business logic. It's methods can have parameters, and can also return results.

Genzy can generate a Web API or an HTTP Client from any Service Class.

Plain

Plain JavaScript classes can be used.

class ExampleService {
async getAll() {
return [];
}
async getById(id) {
return [];
}
async add(example) {
return example;
}
async update(example) {
return example;
}
async delete(id) {
return { id };
}
}

Configured

If you'd like the service, and its methods to be registered on a different route path from the default ones, you can customize them in a $genzy property.

If you're using TypeScript you can define configuration using TypeScript decorators.

note

If you're using decorators, make sure that you've set "experimentalDecorators" option to true in your tsconfig.json file.

class ExampleService {
$genzy = {
path: '/',
getAll: {
httpMethod: 'GET',
path: '/'
},
getById: {
httpMethod: 'GET',
path: '/:id'
},
add: {
httpMethod: 'POST',
path: '/'
},
update: {
httpMethod: 'PUT',
path: '/'
},
delete: {
httpMethod: 'DELETE',
path: '/:id'
},
}

async getAll() {
return [];
}
async getById(id) {
return [];
}
async add(example) {
return example;
}
async update(example) {
return example;
}
async delete(id) {
return { id };
}
}
info

Configuration must be used both on the client and the server side, since it is used for telling Genzy how and where to send the requests, or register the API routes.