Skip to content
Duart Snel edited this page Aug 25, 2020 · 1 revision

Building and understanding models

Models are methods exported from a file that returns a "Model" data type.

Each Model contains an HTTP key with a vlue of type "HTTPModelObject". The HTTPModelObject can take any number of key value pairs with the values being of type "HTTPModelMethod".

Below is an example of the HTTPModelMethod

export interface HTTPModelMethod {
  route: string;
  headers: HeaderEntries
  type: HTTP;
  /**
   * Indicates wether we want to decode the body (from int8arr) before we forward to the relay server
   */
  decode?: boolean;
  /**
   * Indicates wether we want to decode the body to JSON before we send it back to the CLIENT
   */
  decodeResponse?: boolean
  /**
   * Used to specifiy what headers are required to continue with the request.. the middleware will discard the request in case a header is missing.
   * An error is returned when there is a missing header
   */
  requiredHeaders?: Array<string>;
  /**
   * Can only be used in combination with requiredHeaders. 
   * If requiredHeaders is not available this setting will do nothing
   */
  discardUnknownHeaders?: boolean
}

Please refer to the model.ts interface file to learn more about all the types in the model.

example

export const sandbox = async (): Promise<Model> => {
  return {
    HTTP: {
      about: {
        route: `http://localhost:3001/ss`,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Headers': '*',
        },
        type: HTTP.GET,
        decode: false
      },
      old: {
        route: `http://localhost:8000/about`,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Headers': '*'
        },
        type: HTTP.POST,
        decode: true,
        decodeResponse: false
      },
    }
  };
};
Clone this wiki locally