gits_strapi 1.0.1 copy "gits_strapi: ^1.0.1" to clipboard
gits_strapi: ^1.0.1 copied to clipboard

this library is used to facilitate integration with Strapi there are several main functions such as GET collection, single, and CRUD besides that we also provide several helpers to make it easier to use.

Gits Strapi

This package is used to make it easier when you want to integrate with the strapi

Gits Strapi #

# Prerequisites

This library uses the gits_http package

# Yout must know

Requests return a response as an object which usually includes the following keys:

  • data: the response data itself, which could be:

    • a single entry, as an object with the following keys:
      • id (number)
      • attributes (object)
      • meta (object)
    • a list of entries, as an array of objects
    • a custom response
  • meta (object): information about pagination, publication state, available locales, etc.

  • error (object, optional): information about any error thrown by the request

# Usage

First of all, initialize the gits_strapi in this way

 final GitsStrapi strapi = GitsStrapi(
      timeout: 30000,
      showLog: true,
      gitsInspector: GitsInspector(),
    );

after that we can use some main functions like

Auth Login #

Used for standard strapi login needs, if there is a need beyond that then use another function

AuthResponse loginResponse = await strapi.login(
        endpoint: Uri.parse("http://10.0.2.2:1337/api/auth/local"),
        body: {"identifier": "identifier", "password": "password"});

Auth Register #

Used for standard strapi register needs, if there is a need beyond that then use another function

AuthResponse registerResponse = await strapi.register(
        endpoint: Uri.parse("http://10.0.2.2:1337/api/auth/local/register"),
        body: {
          "username": "username",
          "email": "email",
          "password": "password"
        });

Get Single Response #

Used to retrieve data for a single object, if you need to retrieve a lot of data use Get Collection Response

 Uri endpointProductOne =
        Uri.parse("http://10.0.2.2:1337/api/products/1").withParam(
      const StrapiRequest(
        populate: ["images"],
      ),
    );

    SingleResponse<DataResponse<ProductResponse>> singleResponse =
        await strapi.getSingle(endpoint: endpointProductOne).then((value) {
      var attr = ProductResponse.fromMap(value.data?.attributes);
      var data = DataResponse(id: value.data?.id, attributes: attr);
      return SingleResponse(data: data, meta: value.meta, error: value.error);
    });

Get Collection Response #

Used to retrieve data based on a list of objects, if necessary retrieve single object data Get Single Response

 Uri endpointProducts = Uri.parse("http://10.0.2.2:1337/api/products")
        .withParam(
            const StrapiRequest(page: 1, pageSize: 3, sort: ['id:desc']));

    CollectionResponse<DataResponse<ProductResponse>> collectionResponse =
        await strapi.getCollection(endpoint: endpointProducts).then((value) {
      var data = <DataResponse<ProductResponse>>[];
      value.data?.forEach((item) {
        data.add(
          DataResponse(
            id: item.id,
            attributes: ProductResponse.fromMap(item.attributes),
          ),
        );
      });
      return CollectionResponse(
          data: data, meta: value.meta, error: value.error);
    });

(Get) Select #

Used for GET method requirements

Response select = await strapi.select(endpoint: Uri.parse("endpoint"));

(POST) Create #

Used for POST method requirements

var insertBody = {
      "data": {
        "name": "name",
        "description": "description",
        "price": 10000,
        "stock": 5,
      }
    };
    Response insert = await strapi.create(
        endpoint: Uri.parse("http://10.0.2.2:1337/api/products"),
        headers: {
          'Authorization':
              'Bearer eyJhxdcfOgJwUwI1NiIsInR5cCI6IkpXVCJ9.eyJaZcIvMiwfaWF0IjoxNjYxMjM2OTgwLCJleHAiOjE2NjM4Mjg5ODB9.Cb-wP8EUPUcwp76VD_IWqsw5nvi9xv0QqH0Ng4EB1UI'
        },
        body: insertBody);

(PUT) Update #

Used for PUT method requirements

var updateBody = {
      "data": {
        "name": "name",
        "description": "description",
        "price": 50000,
        "stock": 3,
      }
    };
    Response updateResponse = await strapi.update(
        id: "1",
        endpoint: Uri.parse("http://10.0.2.2:1337/api/products"),
        headers: {
          'Authorization':
              'Bearer eyJhbGciOiJIazIwdivsPnRKDCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNjYxMjM2OTUCLVJleHAiOjE2NjM4Mjg5ODB9.Cb-wP8EUPUcwp76VD_IWqsw5nvi9xv0QqH0Ng4EB1UI'
        },
        body: updateBody)

Delete #

Used for DELETE method requirement

 Response deleteResponse = await strapi.delete(headers: {
      'Authorization':
          'Bearer eyJhbGciOiJIUzI1NiIsInCX5FCI6IkpXVCJ9.eyJpZCI6MiwiaWF0IjoxNjYxMjM2OTgwLCJleHA_1OjE2NjM4Mjg5ODB9.Cb-wP8EUPUcwp76VD_IWWsC5GFvi9xv0QqH0Ng4EB1UI'
    }, endpoint: Uri.parse("http://10.0.2.2:1337/api/products"), id: "9");

# Helper

Strapi Request #

we have provided some required parameter requests such as:

  • populate `List
  • fields `List
  • sort `List
  • withCount `bool`
  • pageSize `pageSize`
  • page `page`

for an example like this

    StrapiRequest(
      fields: ['id','name'],
      populate: ['images','transactions'],
      sort: ['id:desc'],
      page: 1,
      pageSize: 10,
      withCount: true,
    );

Entity Mapper #

As for the need to convert Response to Entity which has been provided for some Base Response below:

  • SingleResponse
  • CollectionResponse
  • DataResponse
  • MetaResponse
  • PaginationResponse
  • ErrorResponse
  • AuthResponse
  • UserResponse
  • ThumbnailResponse
  • FormatsResponse
  • ImageResponse

how to use it like this for responses which have generic class

var collectionEntity = collectionResponse.toEntity(
      (data) => data
          .map((item) => (item as DataResponse)
              .toEntity((attr) => (attr as ProductResponse).toEntity()))
          .toList(),
    );

if it doesn't have a generic class

    ProductResponse().toEntity();

Additional information #

For additional information we have several main response bases that will be used frequently

  • SingleResponse : to handle response {"data (single object)","meta","error"}
  • CollectionResponse : to handle response {"data (is an array object)","meta","error"}
  • DataResponse : to handle response {"id","attributes"}
4
likes
110
pub points
1%
popularity

Publisher

verified publishergits.id

this library is used to facilitate integration with Strapi there are several main functions such as GET collection, single, and CRUD besides that we also provide several helpers to make it easier to use.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, gits_http

More

Packages that depend on gits_strapi