@sasjs/adapter 4.0.1 → 4.1.1

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/package.json CHANGED
@@ -79,12 +79,12 @@
79
79
  },
80
80
  "main": "index.js",
81
81
  "dependencies": {
82
- "@sasjs/utils": "2.48.2",
82
+ "@sasjs/utils": "2.52.0",
83
83
  "axios": "0.26.0",
84
84
  "axios-cookiejar-support": "1.0.1",
85
85
  "form-data": "4.0.0",
86
86
  "https": "1.0.0",
87
87
  "tough-cookie": "4.0.0"
88
88
  },
89
- "version": "4.0.1"
89
+ "version": "4.1.1"
90
90
  }
@@ -1,3 +1,4 @@
1
+ import * as NodeFormData from 'form-data'
1
2
  import { AuthConfig, ServerType, ServicePackSASjs } from '@sasjs/utils/types'
2
3
  import { ExecutionQuery } from './types'
3
4
  import { RequestClient } from './request/RequestClient'
@@ -8,6 +9,18 @@ import { getTokens } from './auth/getTokens'
8
9
  export class SASjsApiClient {
9
10
  constructor(private requestClient: RequestClient) {}
10
11
 
12
+ private async getAccessTokenForRequest(authConfig?: AuthConfig) {
13
+ if (authConfig) {
14
+ const { access_token } = await getTokens(
15
+ this.requestClient,
16
+ authConfig,
17
+ ServerType.Sasjs
18
+ )
19
+
20
+ return access_token
21
+ }
22
+ }
23
+
11
24
  /**
12
25
  * Creates the folders and services at the given location `appLoc` on the given server `serverUrl`.
13
26
  * @param dataJson - the JSON specifying the folders and files to be created, can also includes
@@ -20,15 +33,7 @@ export class SASjsApiClient {
20
33
  appLoc: string,
21
34
  authConfig?: AuthConfig
22
35
  ) {
23
- let access_token = (authConfig || {}).access_token
24
- if (authConfig) {
25
- ;({ access_token } = await getTokens(
26
- this.requestClient,
27
- authConfig,
28
- ServerType.Sasjs
29
- ))
30
- }
31
-
36
+ const access_token = await this.getAccessTokenForRequest(authConfig)
32
37
  dataJson.appLoc = dataJson.appLoc || appLoc
33
38
 
34
39
  const { result } = await this.requestClient.post<{
@@ -48,6 +53,40 @@ export class SASjsApiClient {
48
53
  return Promise.resolve(result)
49
54
  }
50
55
 
56
+ /**
57
+ * Creates/updates files within SASjs drive using uploaded json compressed file.
58
+ * @param zipFilePath - Compressed file path; file should only contain one JSON file and
59
+ * should have same name as of compressed file e.g. deploy.JSON should be compressed to deploy.JSON.zip
60
+ * Any other file or JSON file in zipped will be ignored!
61
+ * @param authConfig - (optional) a valid client, secret, refresh and access tokens that are authorised to execute compute jobs.
62
+ */
63
+ public async deployZipFile(zipFilePath: string, authConfig?: AuthConfig) {
64
+ const { createReadStream } = require('@sasjs/utils/file')
65
+ const access_token = await this.getAccessTokenForRequest(authConfig)
66
+
67
+ const file = await createReadStream(zipFilePath)
68
+ const formData = new NodeFormData()
69
+ formData.append('file', file)
70
+
71
+ const contentType = `multipart/form-data; boundary=${formData.getBoundary()}`
72
+
73
+ const { result } = await this.requestClient.post<{
74
+ status: string
75
+ message: string
76
+ streamServiceName?: string
77
+ example?: {}
78
+ }>(
79
+ 'SASjsApi/drive/deploy/upload',
80
+ formData,
81
+ access_token,
82
+ contentType,
83
+ {},
84
+ { maxContentLength: Infinity, maxBodyLength: Infinity }
85
+ )
86
+
87
+ return Promise.resolve(result)
88
+ }
89
+
51
90
  public async executeJob(
52
91
  query: ExecutionQuery,
53
92
  appLoc: string,
@@ -80,14 +119,7 @@ export class SASjsApiClient {
80
119
  runTime: string = 'sas',
81
120
  authConfig?: AuthConfig
82
121
  ) {
83
- let access_token = (authConfig || {}).access_token
84
- if (authConfig) {
85
- ;({ access_token } = await getTokens(
86
- this.requestClient,
87
- authConfig,
88
- ServerType.Sasjs
89
- ))
90
- }
122
+ const access_token = await this.getAccessTokenForRequest(authConfig)
91
123
 
92
124
  let parsedSasjsServerLog = ''
93
125