1.2.4 • Published 10 months ago

@privatus/strapi v1.2.4

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

@privatus/strapi

Strapi extension for Privatus

Recommended version

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

Strapi VerionLibrary Version
4.15.01.0.6
4.16.21.1.0
4.20.21.2.1
4.24.21.2.2
4.25.61.2.3
4.25.91.2.4

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);
1.2.4

10 months ago

1.2.3

12 months ago

1.2.2

1 year ago

1.2.0

1 year ago

1.2.1

1 year ago

1.1.0

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago