Interface ApiXMethod<QuerySchema, BodySchema>

Interface for an ApiXMethod.

Represents an endpoint in you an API-X-based RESTful API. Your endpoint is reaches at /{entity}/{method}. If an entity is not defined, then it will be /{method}.

interface ApiXMethod<QuerySchema, BodySchema> {
    characteristics: ReadonlySet<ApiXMethodCharacteristic>;
    entity?: string;
    httpMethod?:
        | "GET"
        | "POST"
        | "PUT"
        | "DELETE"
        | "PATCH"
        | "ALL";
    jsonBodyRequired?: boolean;
    jsonBodyValidator?: ApiXHttpBodyValidator<BodySchema>;
    method: string;
    queryParameters?: readonly ApiXUrlQueryParameter<unknown>[];
    requestHandler: ApiXRequestHandler<QuerySchema, BodySchema>;
    requestorOwnsResource?: ((request: ApiXRequest<QuerySchema, BodySchema>) => boolean | Promise<boolean>);
}

Type Parameters

Developer Documentation

Handling Query Parameters

Handling Request JSON Body

Handling Requests

Managing Access Control

Setting Endpoint Path

Setting HTTP Method

Handling Query Parameters

queryParameters?: readonly ApiXUrlQueryParameter<unknown>[]

A list of defined query parameters for the method.

If this list is non-empty, the API-X manager will verify that the parameters are valid, required parameters are present and non-empty, and process them.

The parsed parameters will be passed to the requestHandler in the template schema set.

Handling Request JSON Body

jsonBodyRequired?: boolean

A boolean value that determines whether the request must include an HTTP JSON body.

jsonBodyValidator?: ApiXHttpBodyValidator<BodySchema>

An object that validates the JSON body of the request. If present and there's an HTTP Body, the request is validated.

Set jsonBodyRequired to true if you want an immediate failure if the body is missing or empty.

Handling Requests

Handlers for the endpoint. The method is expected to return a JSON response.

All validation and verification is performed before the request reaches your handler. However, if the need arises, you can perform any application-specific verification here as well.

Managing Access Control

characteristics: ReadonlySet<ApiXMethodCharacteristic>

The characteristics that this endpoint has. API-X uses these characteristics to determine what level of access a requestor has to have to access the endpoint.

This set must not be empty.

requestorOwnsResource?: ((request: ApiXRequest<QuerySchema, BodySchema>) => boolean | Promise<boolean>)

A function that determines whether the requestor (request) owns the resource they are asking to access from this endpoint.

The resource is the data or operation that the endpoint provides. The requestor is the identity of the application or user asking for the resource.

This must be implemented for endpoints that provide owned resources, i.e., have the ApiXMethodCharacteristic.PublicOwnedData or ApiXMethodCharacteristic.PrivateOwnedData, otherwise it'll throw an error when attempting to register method.

Type declaration

    • (request): boolean | Promise<boolean>
    • Parameters

      Returns boolean | Promise<boolean>

      true if the requestor owns it, false otherwise.

// An API-X Endpoint that returns data for a given user.
const getUserData = {
endpoint: 'users',
method: ':uid',
...,
requestorOwnsResource: (req) => {
const requestedUserId = req.params.uid;
const userId = dataManager.getAuthenticatedUserId(req);
return userId === requestedUserId;
}
}

Setting Endpoint Path

entity?: string

An optional entity for your endpoint.

Entities are the first path part in your endpoints.

  • /{entity}/{method} - if an entity is defined.

Entities are a great way to scope your methods. For example, user-based methods can be under the user entity, and your method can define the kind of operation to perform on the user entity:

  • /user/new/ - defines an operation that creates a new user.
  • /user/delete/ - defines an operation to delete a new user.
  • /user/:id - defines an operation to get data on an arbitrary user.

It is recommended that an entity is used for all methods.

method: string

The method name for your endpoint. This is typically used to define the operation of your endpoint, and if no entity is provided, it is the first path part in your endpoint. Otherwise, it goes after entity.

Assuming an entity of user, new methods can be created to define operations that can be made on users:

  • /user/new/ - defines an operation that creates a new user (method = new).
  • /user/delete/ - defines an operation to delete a new user (method = delete).
  • /user/:id - defines an operation to get data on an arbitrary user (method = :id).

As seen in the example above, a method can have a template for a parameter, such as ":id".

Setting HTTP Method

httpMethod?:
    | "GET"
    | "POST"
    | "PUT"
    | "DELETE"
    | "PATCH"
    | "ALL"

The HTTP method of your endpoint.