@privatus/strapi
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

@privatus/strapi

Strapi extension for Privatus

Recommended version

Look at this table to consider which version to use by strapi version

Strapi Verion Library Version
4.15.0 1.0.6
4.16.2 1.1.0
4.20.2 1.2.1

Usage

Example Setup

1. Extend content type

import { EntityService } from "@strapi/strapi";
import { FindOneParameters } from "@privatus/strapi";

export type TExampleCollectionTypeContentTypeUID =
  "api::example-collection-type.example-collection-type";
export const ExampleCollectionTypeContentTypeUID: TExampleCollectionTypeContentTypeUID =
  "api::example-collection-type.example-collection-type";

export type ExampleCollectionTypeEntity<
  TParams extends FindOneParameters<TExampleCollectionTypeContentTypeUID> = never
> = EntityService.Entity<TExampleCollectionTypeContentTypeUID, TParams>;

2. Extend service

Collection type

import { BaseCollectionTypeService } from "@privatus/strapi";
import {
  ExampleCollectionTypeEntity,
  TExampleCollectionTypeContentTypeUID,
} from "../content-types/types";
import { Entity } from "@strapi/strapi";

export type ExampleCollectionTypeServiceExtension = {};

export type ExampleCollectionTypeServiceExtension = {
  getById: (id: Entity.ID) => Promise<ExampleCollectionTypeEntity>;
};

export type ExampleCollectionTypeService =
  BaseCollectionTypeService<TExampleCollectionTypeContentTypeUID> &
    ExampleCollectionTypeServiceExtension;

Single type

import { BaseSingleTypeService } from "@privatus/strapi";
import {
  ExampleSingleTypeEntity,
  TExampleSingleTypeContentTypeUID,
} from "../content-types/types";
import { Entity } from "@strapi/strapi";

export type ExampleSingleTypeServiceExtension = {
  getById: (id: Entity.ID) => Promise<ExampleSingleTypeEntity>;
};

export type ExampleSingleTypeService =
  BaseSingleTypeService<TExampleSingleTypeContentTypeUID> &
    ExampleSingleTypeServiceExtension;

3. Implement service extension

import { factories } from "@strapi/strapi";
import { strapiEntityServices } from "../../../../utils/types/strapi";
import { ExampleCollectionTypeTypeUID } from "../content-types/types";

export default factories.createCoreService(
  ExampleCollectionTypeTypeUID,
  ({ strapi }): ExampleCollectionTypeServiceExtension => ({
    getById: async (id: Entity.ID) => {
      return strapiEntityServices(strapi).exampleCollectionType.findOne(id);
    },
  })
);

4. Create alias file

import { Strapi } from "@strapi/strapi";
import { getEntityServiceAlias, strapiWrapper } from "@privatus/strapi";

import { ExampleCollectionTypeContentTypeUID } from "../../../src/api/example-collection-type/content-types/types";
import {
  ExampleCollectionTypeContentTypeUID,
  TExampleCollectionTypeContentTypeUID,
} from "../../../src/api/example-collection-type/content-types/types";
import { ExampleCollectionTypeService } from "../../../src/api/example-collection-type/services/types";

import {
  ExampleSingleTypeContentTypeUID,
  TExampleSingleTypeContentTypeUID,
} from "../../../src/api/example-single-type/content-types/types";
import { ExampleSingleTypeService } from "../../../src/api/example-single-type/services/types";

export function strapiQueries(strapi: Strapi) {
  const wrapper = strapiWrapper(strapi);

  return {
    exampleCollectionType: wrapper.db.query(
      ExampleCollectionTypeContentTypeUID
    ),
  };
}

export function strapiEntityServices(strapi: Strapi) {
  const wrapper = strapiWrapper(strapi);

  return {
    exampleCollectionType: getEntityServiceAlias(
      wrapper,
      ExampleCollectionTypeContentTypeUID
    ),
    exampleSingleType: getEntityServiceAlias(
      wrapper,
      ExampleSingleTypeContentTypeUID
    ),
    plugin: {},
  };
}

export function strapiServices(strapi: Strapi) {
  const wrapper = strapiWrapper(strapi);

  return {
    exampleCollectionType: wrapper.service<
      TExampleCollectionTypeContentTypeUID,
      ExampleCollectionTypeService
    >(ExampleCollectionTypeContentTypeUID),
    exampleSingleType: wrapper.service<
      TExampleSingleTypeContentTypeUID,
      ExampleSingleTypeService
    >(ExampleSingleTypeContentTypeUID),
    plugin: {
      userPermissions: {
        jwt: wrapper.service("plugin::users-permissions.jwt"),
      },
    },
  };
}

Example Usage

const entity = await strapiServices(strapi).exampleCollectionType.getById(1);

Readme

Keywords

none

Package Sidebar

Install

npm i @privatus/strapi

Weekly Downloads

10

Version

1.2.1

License

MIT

Unpacked Size

272 kB

Total Files

81

Last publish

Collaborators

  • aruk_privatus