@cuboapp/api-backend 1.0.17 → 1.0.19

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +9 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuboapp/api-backend",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "Backend Api for CuboApp",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@github.com:cuboapp/api-backend.git",
package/src/index.ts CHANGED
@@ -19,9 +19,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
19
19
 
20
20
  async init() {
21
21
  try {
22
- this.auth = await this.request<{ variables: Record<string, any> }>('/auth/me?with=variables', {
23
- headers: this.options.api?.headers || {}
24
- })
22
+ this.auth = await this.request<{ variables: Record<string, any> }>('/auth/me?with=variables')
25
23
 
26
24
  if (!this.auth) {
27
25
  throw { code: 403, text: 'Авторизация Cubo-Backend не пройдена' }
@@ -40,13 +38,17 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
40
38
  }
41
39
  }
42
40
 
43
- public async request<T>(url: string, opts?: RequestInit & { debug?: boolean; withoutContentType?: boolean }) {
41
+ public async request<T>(url: string, opts?: RequestInit & { debug?: boolean; withoutAuth?: boolean; withoutContentType?: boolean }) {
44
42
  const baseUrl = this.options?.api?.base_url || 'https://api.cubo.sh'
45
43
 
46
44
  const headers: Record<string, any> = {
47
45
  ...(opts?.headers || {})
48
46
  }
49
47
 
48
+ if (!opts?.withoutAuth) {
49
+ Object.assign(headers, this.options.api?.headers || {})
50
+ }
51
+
50
52
  if (!headers['Content-Type'] && !opts?.withoutContentType) {
51
53
  headers['Accept'] = 'application/json'
52
54
  headers['Content-Type'] = 'application/json'
@@ -137,7 +139,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
137
139
  const regularQuery = await this.helpers.createFindQuery(req, entity, queryDto)
138
140
  const countQuery = await this.helpers.createFindQuery(req, entity, queryDto, true)
139
141
 
140
- const db = await this.helpers.getConnection('read')
142
+ const db = await this.helpers.getConnection('write')
141
143
 
142
144
  if (opts?.log) {
143
145
  console.log(`QUERY:getMany to "${String(entityAlias)}":`, regularQuery.sql, regularQuery.replacements, { req, opts, queryDto })
@@ -250,7 +252,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
250
252
  response = await augmentation.afterCreate(response, req, opts)
251
253
  }
252
254
 
253
- if (opts.crdt !== false && this.crdt.isCrdtSupported(entity)) {
255
+ if (opts.crdt === true && this.crdt?.isCrdtSupported(entity)) {
254
256
  const state = await this.crdt.createState({ entityAlias, id: created_id, entity }, response)
255
257
 
256
258
  const fieldKey = entity.fields.find((f) => f.type === 'binary').alias
@@ -302,7 +304,7 @@ export class CuboBackendApi<T extends CuboApiEntitiesMap<T>> {
302
304
  response = await augmentation.afterUpdate(response, req, opts)
303
305
  }
304
306
 
305
- if (opts.crdt !== false && this.crdt.isCrdtSupported(entity)) {
307
+ if (opts.crdt === true && this.crdt?.isCrdtSupported(entity)) {
306
308
  const context = { auth: opts?.extra?.auth }
307
309
  await this.crdt.updateDocument({ entityAlias, id: item.id, entity }, response, context)
308
310