@malevich-studio/strapi-sdk-typescript 1.2.15 → 1.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.cjs CHANGED
@@ -21460,17 +21460,22 @@ class Strapi {
21460
21460
  log(data);
21461
21461
  return data;
21462
21462
  }
21463
- }
21464
-
21465
- /**
21466
- * Returns the TypeScript interface name from a component UID
21467
- * e.g. "default.test-component" => "DefaultTestComponent"
21468
- */
21469
- function getComponentName(uid) {
21470
- return uid
21471
- .split(/[\.\-]/)
21472
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
21473
- .join('');
21463
+ async can(uid, controller, action) {
21464
+ if (!this.permissions) {
21465
+ const response = await this.fetchData('users-permissions/permissions');
21466
+ this.permissions = response.permissions;
21467
+ }
21468
+ if (!this.permissions[uid]) {
21469
+ throw new Error(`Permissions for ${uid} not found!`);
21470
+ }
21471
+ if (!this.permissions[uid].controllers[controller]) {
21472
+ throw new Error(`Permissions for ${uid}.${controller} not found!`);
21473
+ }
21474
+ if (!this.permissions[uid].controllers[controller][action]) {
21475
+ throw new Error(`Permission for ${uid}.${controller}.${action} not found!`);
21476
+ }
21477
+ return this.permissions[uid].controllers[controller][action].enabled;
21478
+ }
21474
21479
  }
21475
21480
 
21476
21481
  var AttributeMode;
@@ -21543,32 +21548,6 @@ class BaseRelation extends Base {
21543
21548
  }
21544
21549
  }
21545
21550
 
21546
- class Media extends BaseRelation {
21547
- constructor(name, attribute) {
21548
- super(name, attribute);
21549
- this.name = name;
21550
- this.attribute = attribute;
21551
- }
21552
- getType() {
21553
- return this.attribute.multiple ? 'File[]' : 'File';
21554
- }
21555
- getInputType() {
21556
- return 'number[]';
21557
- }
21558
- getPopulates() {
21559
- return [{
21560
- name: this.name,
21561
- type: 'FileQuery',
21562
- }];
21563
- }
21564
- getFilters() {
21565
- return [{
21566
- name: this.name,
21567
- type: 'FileFilters',
21568
- }];
21569
- }
21570
- }
21571
-
21572
21551
  /**
21573
21552
  * Returns the TypeScript interface name from a content type UID
21574
21553
  * e.g. "api::article.article" => "Article"
@@ -21643,6 +21622,43 @@ class Relation extends BaseRelation {
21643
21622
  }
21644
21623
  }
21645
21624
 
21625
+ /**
21626
+ * Returns the TypeScript interface name from a component UID
21627
+ * e.g. "default.test-component" => "DefaultTestComponent"
21628
+ */
21629
+ function getComponentName(uid) {
21630
+ return uid
21631
+ .split(/[\.\-]/)
21632
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
21633
+ .join('');
21634
+ }
21635
+
21636
+ class Media extends BaseRelation {
21637
+ constructor(name, attribute) {
21638
+ super(name, attribute);
21639
+ this.name = name;
21640
+ this.attribute = attribute;
21641
+ }
21642
+ getType() {
21643
+ return this.attribute.multiple ? 'File[]' : 'File';
21644
+ }
21645
+ getInputType() {
21646
+ return 'number[]';
21647
+ }
21648
+ getPopulates() {
21649
+ return [{
21650
+ name: this.name,
21651
+ type: 'FileQuery',
21652
+ }];
21653
+ }
21654
+ getFilters() {
21655
+ return [{
21656
+ name: this.name,
21657
+ type: 'FileFilters',
21658
+ }];
21659
+ }
21660
+ }
21661
+
21646
21662
  class Enumeration extends Base {
21647
21663
  constructor(name, attribute) {
21648
21664
  super(name, attribute);
@@ -21952,14 +21968,22 @@ function generateMethodsCode(contentType) {
21952
21968
  ` return await this.delete<${modelName}>('${contentType.schema.pluralName}', id, params);`,
21953
21969
  ' }',
21954
21970
  ].join('\n'));
21971
+ console.log(contentType.uid);
21972
+ if (contentType.uid.startsWith('api::')) {
21973
+ methods.push([
21974
+ ` public async can${getContentTypeName(contentType.schema.singularName)}(action: PermissionAction) {`,
21975
+ ` return await this.can('${contentType.uid}', '${contentType.schema.singularName.replace(/^api::/, '')}', action);`,
21976
+ ' }',
21977
+ ].join('\n'));
21978
+ }
21955
21979
  return methods;
21956
21980
  }
21957
21981
  /**
21958
21982
  * Main function to fetch Strapi (v5) data and generate the d.ts file
21959
21983
  */
21960
21984
  async function generateStrapiTypes(strapi) {
21961
- const contentTypes = (await strapi.fetch('content-type-builder/content-types')).data;
21962
- const components = (await strapi.fetch('content-type-builder/components')).data;
21985
+ const contentTypes = (await strapi.fetch('content-type-builder/content-types')).data || [];
21986
+ const components = (await strapi.fetch('content-type-builder/components')).data || [];
21963
21987
  const allInterfaces = [];
21964
21988
  const methods = [];
21965
21989
  for (const component of components) {
@@ -22002,6 +22026,12 @@ async function generateStrapiTypes(strapi) {
22002
22026
  locale: {
22003
22027
  type: 'string',
22004
22028
  },
22029
+ localizations: {
22030
+ type: 'relation',
22031
+ target: contentType.uid,
22032
+ relation: AttributeRelation.OneToMany,
22033
+ required: true,
22034
+ },
22005
22035
  } : {}),
22006
22036
  ...contentType.schema.attributes,
22007
22037
  };
@@ -22010,7 +22040,7 @@ async function generateStrapiTypes(strapi) {
22010
22040
  allInterfaces.push(generateInputTypeCode(modelName, attributes));
22011
22041
  }
22012
22042
  const output = [
22013
- 'import {Strapi as StrapiBase, Query, Filters, FilterValue, RelationInput, DynamiczonePopulate, DynamiczoneComponent} from "@malevich-studio/strapi-sdk-typescript";',
22043
+ 'import {Strapi as StrapiBase, Query, Filters, FilterValue, RelationInput, DynamiczonePopulate, DynamiczoneComponent, PermissionAction} from "@malevich-studio/strapi-sdk-typescript";',
22014
22044
  'import {BlocksContent} from "@strapi/blocks-react-renderer";',
22015
22045
  '',
22016
22046
  'export default class Strapi extends StrapiBase {',