@malevich-studio/strapi-sdk-typescript 1.2.2 → 1.2.4

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/index.d.ts CHANGED
@@ -41,6 +41,11 @@ type ErrorResponse = {
41
41
  };
42
42
  };
43
43
  type Response<T> = SuccessResponse<T> | ErrorResponse;
44
+ type AuthResponse<T> = {
45
+ jwt: string;
46
+ user: T;
47
+ error: undefined;
48
+ } | ErrorResponse;
44
49
  type File = {
45
50
  id?: number;
46
51
  documentId?: string;
@@ -141,9 +146,25 @@ type DynamiczonePopulate<T> = {
141
146
  };
142
147
  declare class Strapi {
143
148
  private readonly url;
144
- private readonly token?;
149
+ private token?;
145
150
  constructor(url: string, token?: string | undefined);
146
151
  fetch<T>(endpoint: string, data?: object | FormData, params?: RequestInit): Promise<Response<T>>;
152
+ private fetchData;
153
+ setToken(token: string): void;
154
+ getToken(): string | undefined;
155
+ protected baseLogin<T>(identifier: string, password: string): Promise<AuthResponse<T>>;
156
+ protected baseRegister<T, Q>(data: Q): Promise<AuthResponse<T>>;
157
+ forgotPassword(email: string): Promise<ErrorResponse | {
158
+ ok: boolean;
159
+ error: undefined;
160
+ }>;
161
+ sendEmailConfirmation(email: string): Promise<ErrorResponse | {
162
+ email: string;
163
+ sent: boolean;
164
+ error: undefined;
165
+ }>;
166
+ protected baseResetPassword<T>(password: string, code: string): Promise<AuthResponse<T>>;
167
+ protected baseChangePassword<T>(password: string, currentPassword: string): Promise<AuthResponse<T>>;
147
168
  getLocales(params: RequestInit): Promise<Locale[]>;
148
169
  getDocuments<T, Q extends object>(endpoint: string, data?: Q, params?: RequestInit): Promise<Response<T[]>>;
149
170
  getDocument<T, Q extends object>(endpoint: string, data?: Q, params?: RequestInit): Promise<Response<T>>;
@@ -154,4 +175,4 @@ declare class Strapi {
154
175
  private baseFetch;
155
176
  }
156
177
 
157
- export { type DynamiczoneComponent, type DynamiczonePopulate, type File, type FilterValue, type Filters, type Folder, type Locale, type Query, type RelationInput, Strapi };
178
+ export { type AuthResponse, type DynamiczoneComponent, type DynamiczonePopulate, type File, type FilterValue, type Filters, type Folder, type Locale, type Query, type RelationInput, Strapi };
package/dist/index.mjs CHANGED
@@ -20077,6 +20077,9 @@ class Strapi {
20077
20077
  this.token = token;
20078
20078
  }
20079
20079
  async fetch(endpoint, data = {}, params = {}) {
20080
+ return await this.fetchData(endpoint, data, params);
20081
+ }
20082
+ async fetchData(endpoint, data = {}, params = {}) {
20080
20083
  const queryString = params.method === 'GET' ? qs.stringify(data) : '';
20081
20084
  return await this.baseFetch(queryString ? `${endpoint}?${queryString}` : endpoint, _.merge({
20082
20085
  headers: {
@@ -20089,6 +20092,69 @@ class Strapi {
20089
20092
  } : {}),
20090
20093
  }, params));
20091
20094
  }
20095
+ setToken(token) {
20096
+ this.token = token;
20097
+ }
20098
+ getToken() {
20099
+ return this.token;
20100
+ }
20101
+ async baseLogin(identifier, password) {
20102
+ const response = await this.fetchData('/auth/local', {
20103
+ identifier,
20104
+ password
20105
+ }, {
20106
+ method: 'POST',
20107
+ });
20108
+ if (!response.error) {
20109
+ this.setToken(response.jwt);
20110
+ }
20111
+ return response;
20112
+ }
20113
+ async baseRegister(data) {
20114
+ const response = await this.fetchData('/auth/local/register', data, {
20115
+ method: 'POST',
20116
+ });
20117
+ if (!response.error) {
20118
+ this.setToken(response.jwt);
20119
+ }
20120
+ return response;
20121
+ }
20122
+ async forgotPassword(email) {
20123
+ return await this.fetchData('/auth/forgot-password', { email }, {
20124
+ method: 'POST',
20125
+ });
20126
+ }
20127
+ async sendEmailConfirmation(email) {
20128
+ return await this.fetchData('/auth/send-email-confirmation', { email }, {
20129
+ method: 'POST',
20130
+ });
20131
+ }
20132
+ async baseResetPassword(password, code) {
20133
+ const response = await this.fetchData('/auth/reset-password', {
20134
+ password,
20135
+ passwordConfirmation: password,
20136
+ code,
20137
+ }, {
20138
+ method: 'POST',
20139
+ });
20140
+ if (!response.error) {
20141
+ this.setToken(response.jwt);
20142
+ }
20143
+ return response;
20144
+ }
20145
+ async baseChangePassword(password, currentPassword) {
20146
+ const response = await this.fetchData('/auth/change-password', {
20147
+ password,
20148
+ passwordConfirmation: password,
20149
+ currentPassword,
20150
+ }, {
20151
+ method: 'POST',
20152
+ });
20153
+ if (!response.error) {
20154
+ this.setToken(response.jwt);
20155
+ }
20156
+ return response;
20157
+ }
20092
20158
  async getLocales(params) {
20093
20159
  return await this.baseFetch('i18n/locales', _.merge({
20094
20160
  headers: {