77-Nodejs Course 2023: Resources Output

hassanzohdy

Hasan Zohdy

Posted on December 1, 2022

77-Nodejs Course 2023: Resources Output

So we saw in our previous chapter the concept of resources, we stopped at defining the output property, now let's start defining it.

Output property

This property output will be an object, that object's key will be both the key that will be returned and also will be used to get the value from the resource as well.

On the other hand, the value of the key will define how the key will be returned in the response.

Let's take an example to make everything clear, let's say we have a user model that has the following data:

export default class Resource {
  /**
   * Constructor
   */
  public constructor(protected resource: any = {}) {
    //
  }

  /**
   * Output shape
   */
  protected output: any = {};
}
Enter fullscreen mode Exit fullscreen mode

Now let's create our first resource, user-resource class

// src/app/users/resources/user-resource.ts
import Resource from 'core/resources/resource';


export default class UserResource extends Resource {
  /**
   * Output shape
   */
  protected output: any = {
    id: 'number',
    name: 'string',
    email: 'string',
    age: 'number',
  };
}
Enter fullscreen mode Exit fullscreen mode

So we just defined here our output property, we defined the key id and its type is number, the key name and its type is string, the key email and its type is string, and the key age and its type is number.

Now let's use this resource in our get-user.ts

// src/app/users/controllers/get-user.ts
import UserResource from "../resources/user-resource";

export default function getUser() {
  return {
    user: new UserResource({
      id: 12,
      name: "Hasan",
      email: "hassanzohdy@gmail.com",
      age: 33,
    }),
  };
}
Enter fullscreen mode Exit fullscreen mode

Now let's open our request in the browser or postman, you will see the same exact data as we passed in the controller, why? because we didn't do anything yet 😂

Now let's start updating our toJSON method in our 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;
      }

      // just for now sett the output value to the data
      data[key] = value;
    }

    return data;
  }
}
Enter fullscreen mode Exit fullscreen mode

We updated our toJSON method as we defined the final data that will be used to return the response, then we looped over the output object.

First thing to do is to get the value type, then we got the value itself from our passed resource.

Note that i defined a missingKey symbol, this is used to make sure that if the key is missing from the resource, we will skip it.

Later i just added it to our defined data object and returned it.

For now you you will not see anything new as i just returned what is exactly sent, now let's add some logic to our code.

// src/core/resources/resource.ts
// ...
  /**
   * 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;
    }
  }
Enter fullscreen mode Exit fullscreen mode

We added here our cast method that receives the value that we want to cast and its type that will be casted into, now let's use it in our code.

But before that, i assume that you're already familiar with that switch statement, i made just a few checks over the type and returned the value after casting it.

// 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);
      }

      // 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;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Now to test our code, just update the get-user.ts controller

// src/app/users/controllers/get-user.ts
import UserResource from "../resources/user-resource";

export default function getUser() {
  return {
    user: new UserResource({
      id: "12",
      name: "Hasan",
      email: "hassanzohdy@gmail.com",
      age: "33",
    }),
  };
}
Enter fullscreen mode Exit fullscreen mode

Now if you open your request in the browser or postman, you will see that the id and age are now numbers, and the name and email are strings.

🎨 Conclusion

We got introduced to our new property output, this was just the beginning, in our next article we'll see how to use custom functions for custom casting.

☕♨️ 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

Packages & Libraries

React Js Packages

Courses (Articles)

💖 💪 🙅 🚩
hassanzohdy
Hasan Zohdy

Posted on December 1, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related