We already defined out output cast types in our previous chapter, now let's see how we can define custom output handlers.
Purpose
Sometimes we want to customize the final output of certain value, for example using uploadsUrl
to generate a full url for the uploaded file as we store only in the database the relative path of uploads directory thus we need to return full url for the uploaded file.
Custom output handler
This is where we can define our custom output handlers, let's take an example to make everything clear, let's update our user resource class.
// src/app/users/resources/user-resource.ts
import Resource from 'core/resources/resource';
import { uploadsUrl } from 'core/utils/urls';
export default class UserResource extends Resource {
/**
* Output shape
*/
protected output: any = {
id: 'number',
name: 'string',
email: 'string',
age: 'number',
avatar: uploadsUrl,
};
}
Of course this will not work as we didn't transform the call of uploadsUrl
to be a function call, so let's do that in our base resource class
// src/core/resources/resource.ts
import { get } from "@mongez/reinforcements";
// this will be used to skip the output property if it is missing from the given resource
const missingKey = Symbol("missing");
export default class Resource {
/**
* Constructor
*/
public constructor(protected resource: any = {}) {
//
}
/**
* Output shape
*/
protected output: any = {};
/**
* {@inheritDoc}
*/
public toJSON() {
// final output
const data: Record<string, any> = {};
// loop through the output property
for (const key in this.output) {
// get the value type
const valueType = this.output[key];
// get the value, and also make sure to skip the output property if it is missing from the given resource
let value = get(this.resource, key, missingKey);
// skip the output property if it is missing from the given resource
if (value === missingKey) {
continue;
}
if (typeof valueType === "string") {
// cast the value
value = this.cast(value, valueType);
} else if (typeof valueType === "function") {
// call the custom output handler
value = valueType(value);
}
// just for now sett the output value to the data
data[key] = value;
}
return data;
}
/**
* Builtin casts
*/
protected cast(value: any, type: string) {
switch (type) {
case "number":
return Number(value);
case "float":
case "double":
return parseFloat(value);
case "int":
case "integer":
return parseInt(value);
case "string":
return String(value);
case "boolean":
return Boolean(value);
default:
return value;
}
}
}
We added another if statement to check the value type, if it is a function then we call it and pass the value to it, and then we return the result.
Now when we check the response, we'll see something like this.
"image": "http://127.0.0.1:3000/uploads/users/my-image.jpg"
As we only have in the resource object, users/my-image.jpg
and we want to return the full url.
🎨 Conclusion
In this chapter, we added another feature which is using custom output handler to mutate the final output value as we want, this will give us much flexibility to customize the final output.
In our next chapter, we'll see how to use nested resources, a resource inside another resource!, stay tuned.
☕♨️ Buy me a Coffee ♨️☕
If you enjoy my articles and see it useful to you, you may buy me a coffee, it will help me to keep going and keep creating more content.
🚀 Project Repository
You can find the latest updates of this project on Github
😍 Join our community
Join our community on Discord to get help and support (Node Js 2023 Channel).
🎞️ Video Course (Arabic Voice)
If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.
📚 Bonus Content 📚
You may have a look at these articles, it will definitely boost your knowledge and productivity.
General Topics
- Event Driven Architecture: A Practical Guide in Javascript
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
Packages & Libraries
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
React Js Packages
Courses (Articles)
Top comments (0)