2. What is a Resource?

First we must define what a Resource means: A resource is a class which extends the following class:

export declare class Resource<T> {
  public id?: number;
  public save(): Promise<T>;
  public remove(): Promise<T>;
  public static one<T>(id: number | string, queryParams?: QueryParams): Promise<T>;
  public static findOne<T>(queryParams: QueryParams): Promise<T | void>;
  public static list<T>(queryParams?: QueryParams): Promise<T[]>;
  public static page<T>(queryParams?: QueryParams): Promise<Page<T>>;

Lets say we want to define a Pokemon resource, we would go about it in the following way:

import { makeResource, Page } from '@42.nl/spring-connect';

export default class Pokemon extends makeResource<Pokemon>('/api/pokemon') {
    These are the properties of the pokemon which it has when it is
    fetched from the back-end. Note that `!`, TypeScript needs this
    to accept that the properties will be there once the resource
    is loaded.

    When creating a `new Pokemon` all properties will actually be
    empty. But this is a nice trade-off to prevent the programmer
    from having to do excessive null checks.

  id!: number;
  name!: string;
  types!: string[];

The argument to makeResource is the baseUrl used to fetch the Resource’ data from the REST API. We have now created a Pokemon Resource, lets see how we can use it.