@malevich-studio/strapi-sdk-typescript 1.2.20 → 1.2.22

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
@@ -21433,6 +21433,12 @@ class Strapi {
21433
21433
  ...params,
21434
21434
  });
21435
21435
  }
21436
+ async updateUser(endpoint, id, data, params = {}) {
21437
+ return await this.fetchData(`${endpoint}/${id}`, data, {
21438
+ method: 'PUT',
21439
+ ...params,
21440
+ });
21441
+ }
21436
21442
  async delete(endpoint, id, params = {}) {
21437
21443
  return await this.fetch(`${endpoint}/${id}`, {}, {
21438
21444
  method: 'DELETE',
@@ -21454,7 +21460,11 @@ class Strapi {
21454
21460
  } : {}),
21455
21461
  }, params);
21456
21462
  const response = await fetch(`${this.url}/api/${endpoint}`, mergedParams);
21457
- const data = await response.json();
21463
+ let data = null;
21464
+ try {
21465
+ data = await response?.json() || null;
21466
+ }
21467
+ catch (error) { }
21458
21468
  log(mergedParams);
21459
21469
  log(response);
21460
21470
  log(data);
@@ -21462,8 +21472,11 @@ class Strapi {
21462
21472
  }
21463
21473
  async can(uid, controller, action) {
21464
21474
  if (!this.permissionsList) {
21465
- const { role } = await this.me({ populate: { role: { fields: ["id"] } } });
21466
- const response = await this.fetchData(`users-permissions/roles/${role?.id}`);
21475
+ const user = await this.baseMe({ populate: { role: { fields: ["id"] } } });
21476
+ if (user.error) {
21477
+ return false;
21478
+ }
21479
+ const response = await this.fetchData(`users-permissions/roles/${user.role.id}`);
21467
21480
  this.permissionsList = response.role.permissions;
21468
21481
  }
21469
21482
  if (!this.permissionsList[uid]) {
@@ -21939,7 +21952,7 @@ function generateInputTypeCode(name, attributes) {
21939
21952
  lines.push(`}`);
21940
21953
  return lines.join('\n');
21941
21954
  }
21942
- function generateMethodsCode(contentType) {
21955
+ function generateMethodsCode(contentType, permissions) {
21943
21956
  const methods = [];
21944
21957
  const modelName = getContentTypeName(contentType.uid);
21945
21958
  if (contentType.schema.kind === ContentTypeKind.CollectionType) {
@@ -21961,7 +21974,7 @@ function generateMethodsCode(contentType) {
21961
21974
  ].join('\n'));
21962
21975
  methods.push([
21963
21976
  ` public async update${getContentTypeName(contentType.schema.singularName)}(id: string, data: ${modelName}Input, params?: RequestInit) {`,
21964
- ` return await this.update<${modelName}, ${modelName}Input>('${contentType.schema.pluralName}', id, data, params);`,
21977
+ ` return await this.update${modelName === 'User' ? 'User' : ''}<${modelName}, ${modelName}Input>('${contentType.schema.pluralName}', id, data, params);`,
21965
21978
  ' }',
21966
21979
  ].join('\n'));
21967
21980
  methods.push([
@@ -21969,10 +21982,10 @@ function generateMethodsCode(contentType) {
21969
21982
  ` return await this.delete<${modelName}>('${contentType.schema.pluralName}', id, params);`,
21970
21983
  ' }',
21971
21984
  ].join('\n'));
21972
- console.log(contentType.uid);
21973
21985
  if (contentType.uid.startsWith('api::')) {
21986
+ const actions = Object.keys(permissions[contentType.uid.split('.')[0]].controllers[contentType.schema.singularName]);
21974
21987
  methods.push([
21975
- ` public async can${getContentTypeName(contentType.schema.singularName)}(action: PermissionAction) {`,
21988
+ ` public async can${getContentTypeName(contentType.schema.singularName)}(action: '${actions.join('\' | \'')}') {`,
21976
21989
  ` return await this.can('${contentType.uid.split('.')[0]}', '${contentType.schema.singularName}', action);`,
21977
21990
  ' }',
21978
21991
  ].join('\n'));
@@ -21985,6 +21998,7 @@ function generateMethodsCode(contentType) {
21985
21998
  async function generateStrapiTypes(strapi) {
21986
21999
  const contentTypes = (await strapi.fetch('content-type-builder/content-types')).data || [];
21987
22000
  const components = (await strapi.fetch('content-type-builder/components')).data || [];
22001
+ const permissions = (await strapi.fetchData('users-permissions/permissions')).permissions || {};
21988
22002
  const allInterfaces = [];
21989
22003
  const methods = [];
21990
22004
  for (const component of components) {
@@ -22003,7 +22017,7 @@ async function generateStrapiTypes(strapi) {
22003
22017
  if (!['api::', 'plugin::upload', 'plugin::users-permissions'].filter(prefix => contentType.uid.startsWith(prefix)).length) {
22004
22018
  continue;
22005
22019
  }
22006
- methods.push(...generateMethodsCode(contentType));
22020
+ methods.push(...generateMethodsCode(contentType, permissions));
22007
22021
  const modelName = getContentTypeName(contentType.uid);
22008
22022
  const attributes = {
22009
22023
  id: {