CRUD API model for a typical DRF ModelViewSet.

Configuration

To configure Crud, injection tokens must be set. Tokens: CRUD_HTTP_CLIENT CRUD_BASE

Action Crud

Use the getAction method to create a new instance of Crud but specific for your action.

For action views, you muse set Crud.actionName.

For detail action views, you must provide Crud.actionDetail and pass the parent PK in every method for API calls.

Generic Type Params

T is the type of data. LT is the type of data returned in list.

Type Parameters

Constructors

Properties

actionDetail: boolean = false

Is this a detailed action (in case of action API)? (default is false).

actionName?: string

Action name in case of action API.

base: string

API base URL (default is CRUD_BASE).

Set this to override the injection token value.

cacheEnable: boolean = false

Enable cache for list action? (default is false).

Cache is reset automatically when making these API calls:

  • create
  • update
  • destroy
  • cache
  • cacheReset
  • list
getContext: GetContextFunction = ...

Method that returns an HTTP context instance to use for making API calls.

mapItem?: MapItemFunction<any>

Method that changes the data from API response.

name: string

API endpoint.

Accessors

Methods

  • Create a new object. ModelViewSet create action.

    Parameters

    Returns Observable<T>

  • Delete a single object. ModelViewSet delete action.

    Parameters

    • pk: PK
    • OptionalparentPk: PK

    Returns Observable<void>

  • Trigger list action with fields needed.

    Parameters

    • fields: (string | keyof T)[]

      Fields to include in the API response.

    • Optionalparams: GetParams

      Rest of the query params (optional).

    • OptionalignoreCache: boolean

      Flag to ignore the cache (default is false).

    • OptionalparentPk: PK

      Parent PK in case of action view (optional).

    Returns Observable<LT>

    cacheEnable

  • Create an action view of this Crud.

    Type Parameters

    Parameters

    • name: string

      Action name.

    • isDetail: boolean

      Whether action is detail.

    • options: Omit<CrudInit<AT, ATL>,
          | "name"
          | "injector"
          | "actionName"
          | "actionDetail"> = {}

      Other Crud options.

    Returns Crud<AT, ATL>

    new Crud instance but action.

  • Parameters

    • OptionalparentPk: PK

      PK of parent in case of action.

    Returns string

    Full API endpoint URL.

    Error when parentPk is not provided in case of detail action.

  • Parameters

    • pk: PK

      PK of the object.

    • OptionalparentPk: PK

      PK of parent in case of action.

    Returns string

    Full API endpoint URL of a single object.

    Error when parentPk is not provided in case of detail action.

  • Get list of objects. ModelViewSet list action.

    Parameters

    • Optionalparams: GetParams

      HTTP query params.

    • OptionalignoreCache: boolean

      Ignore cached value (if cache is enabled) (default is false).

    • OptionalparentPk: PK

      PK of parent object (if this is an action Crud) (optional).

    Returns Observable<LT>

  • Get a single object. ModelViewSet retrieve action.

    Parameters

    • pk: PK
    • OptionalparentPk: PK

    Returns Observable<T>

  • Update a single object. ModelViewSet update action.

    Parameters

    Returns Observable<T>

  • Create a new file upload object.

    Same as create but with FormData as payload.

    Parameters

    • file: File

      File instance to append to the form. Uses name for the filename property of append.

    • payload: FormData = ...

      Form data instance (defaults to new FormData()).

    • OptionalparentPk: PK

      PK of the parent in case of detail action CRUD instance.

    Returns Observable<T>