@cdek-it/widget 3.8.1 → 3.9.0

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/service.php CHANGED
@@ -71,11 +71,12 @@ class service
71
71
 
72
72
  private function getAuthToken()
73
73
  {
74
- $result = json_decode($this->httpRequest('oauth/token', array(
74
+ $token = $this->httpRequest('oauth/token', array(
75
75
  'grant_type' => 'client_credentials',
76
76
  'client_id' => $this->login,
77
77
  'client_secret' => $this->secret,
78
- ), true), true);
78
+ ), true);
79
+ $result = json_decode($token['result'], true);
79
80
  if (!isset($result['access_token'])) {
80
81
  throw new RuntimeException('Server not authorized to CDEK API');
81
82
  }
@@ -114,22 +115,46 @@ class service
114
115
  CURLOPT_USERAGENT => 'widget/2.0',
115
116
  CURLOPT_HTTPHEADER => $headers,
116
117
  CURLOPT_RETURNTRANSFER => true,
118
+ CURLOPT_HEADER => true,
117
119
  ));
118
120
 
119
- $result = curl_exec($ch);
120
-
121
+ $response = curl_exec($ch);
122
+ $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
123
+ $headers = substr($response, 0, $headerSize);
124
+ $result = substr($response, $headerSize);
125
+ $addedHeaders = $this->getHeaderValue($headers);
121
126
  if ($result === false) {
122
127
  throw new RuntimeException(curl_error($ch), curl_errno($ch));
123
128
  }
124
129
 
125
- return $result;
130
+ return array('result' => $result, 'addedHeaders' => $addedHeaders);
131
+ }
132
+
133
+ private function getHeaderValue($headers)
134
+ {
135
+ $headerLines = explode("\r\n", $headers);
136
+ $addedHeaders = [];
137
+ foreach ($headerLines as $line) {
138
+ if (!empty($line)) {
139
+ $parts = explode(': ', $line, 2);
140
+ $key = $parts[0];
141
+ $value = isset($parts[1]) ? $parts[1] : '';
142
+ $addedHeaders[$key] = $value;
143
+ }
144
+ }
145
+ return $addedHeaders;
126
146
  }
127
147
 
128
148
  private function sendResponse($data)
129
149
  {
130
150
  $this->http_response_code(200);
131
151
  header('Content-Type: application/json');
132
- echo $data;
152
+ if (!empty($data['addedHeaders'])) {
153
+ foreach ($data['addedHeaders'] as $headerName => $headerValue) {
154
+ header($headerName.': '.$headerValue);
155
+ }
156
+ }
157
+ echo $data['result'];
133
158
  exit();
134
159
  }
135
160
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cdek-it/widget",
3
- "version": "3.8.1",
3
+ "version": "3.9.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -14,6 +14,22 @@
14
14
  "require": "./dist/cdek-widget.umd.js"
15
15
  }
16
16
  },
17
+ "repository": "github:cdek-it/widget",
18
+ "homepage": "https://widget.cdek.ru",
19
+ "bugs": {
20
+ "url": "https://github.com/cdek-it/widget/issues",
21
+ "email": "integrator@cdek.ru"
22
+ },
23
+ "author": {
24
+ "email": "a.viarvelskii@cdek.ru",
25
+ "name": "Alex V"
26
+ },
27
+ "contributors": [
28
+ {
29
+ "email": "i.klementev@cdek.ru",
30
+ "name": "Ilya Klementev"
31
+ }
32
+ ],
17
33
  "publishConfig": {
18
34
  "access": "public"
19
35
  },