@vtecx/vtecxnext 3.0.6 → 3.1.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.
@@ -148,6 +148,8 @@ export declare class VtecxNext {
148
148
  private loginCookies;
149
149
  /** next cookies (for server action) */
150
150
  private cookieStore;
151
+ /** Whether server action cookies() should be used */
152
+ private useCookieStore;
151
153
  /** flag whether the next cookies should be created */
152
154
  private shouldBeCreatedCookieStore;
153
155
  /**
@@ -156,6 +158,7 @@ export declare class VtecxNext {
156
158
  * @param accessToken Access token (for batch)
157
159
  */
158
160
  constructor(req?: NextRequest, accessToken?: string);
161
+ constructor(accessToken?: string);
159
162
  /**
160
163
  * Initial processing for batch.
161
164
  */
package/dist/vtecxnext.js CHANGED
@@ -59,23 +59,26 @@ class VtecxNext {
59
59
  loginCookies = {};
60
60
  /** next cookies (for server action) */
61
61
  cookieStore; // ReadonlyRequestCookies
62
+ /** Whether server action cookies() should be used */
63
+ useCookieStore = false;
62
64
  /** flag whether the next cookies should be created */
63
- shouldBeCreatedCookieStore = true;
64
- /**
65
- * constructor
66
- * @param req Request (for api)
67
- * @param accessToken Access token (for batch)
68
- */
69
- constructor(req, accessToken) {
70
- if (req) {
71
- this.req = req;
72
- this.shouldBeCreatedCookieStore = false;
73
- }
74
- else {
65
+ shouldBeCreatedCookieStore = false;
66
+ constructor(reqOrAccessToken, accessToken) {
67
+ if (typeof reqOrAccessToken === 'string') {
75
68
  this.req = undefined;
76
- this.accessToken = accessToken;
69
+ this.accessToken = reqOrAccessToken;
77
70
  this.useAccessToken = true;
71
+ return;
78
72
  }
73
+ if (reqOrAccessToken) {
74
+ this.req = reqOrAccessToken;
75
+ return;
76
+ }
77
+ this.req = undefined;
78
+ this.accessToken = accessToken;
79
+ this.useAccessToken = accessToken ? true : undefined;
80
+ this.useCookieStore = true;
81
+ this.shouldBeCreatedCookieStore = true;
79
82
  }
80
83
  /**
81
84
  * Initial processing for batch.
@@ -96,6 +99,8 @@ class VtecxNext {
96
99
  //console.log(`[vtecxnext init] response. status=${response.status}`)
97
100
  // vte.cxからのset-cookieを転記
98
101
  this.setCookie(response);
102
+ // バッチ実行用にセッションCookieを保持する
103
+ this.setLoginCookie(response);
99
104
  // レスポンスのエラーチェック
100
105
  await checkVtecxResponse(response);
101
106
  // 以降アクセストークンは使用しない
@@ -4179,12 +4184,18 @@ class VtecxNext {
4179
4184
  */
4180
4185
  setLoginCookie = (response) => {
4181
4186
  // set-cookieの値をレスポンスヘッダ格納変数にセット
4182
- let setCookieVal = response.headers.get('set-cookie');
4187
+ const setCookieVal = response.headers.get('set-cookie');
4183
4188
  if (setCookieVal) {
4184
- const tmpCookies = setCookieVal.split(';');
4185
- for (const tmpCookie of tmpCookies) {
4186
- const tmpKeyVal = tmpCookie.split('=');
4187
- this.loginCookies[tmpKeyVal[0]] = tmpKeyVal[1];
4189
+ for (const cookie of splitSetCookieHeader(setCookieVal)) {
4190
+ const idx = cookie.indexOf('=');
4191
+ if (idx <= 0) {
4192
+ continue;
4193
+ }
4194
+ const name = cookie.substring(0, idx).trim();
4195
+ const value = cookie.substring(idx + 1).trim();
4196
+ if (name) {
4197
+ this.loginCookies[name] = value;
4198
+ }
4188
4199
  }
4189
4200
  }
4190
4201
  };
@@ -4211,7 +4222,7 @@ class VtecxNext {
4211
4222
  }
4212
4223
  }
4213
4224
  else {
4214
- if (this.shouldBeCreatedCookieStore) {
4225
+ if (this.useCookieStore && this.shouldBeCreatedCookieStore) {
4215
4226
  //console.log(`[editRequestCookie] shouldBeCreatedCookieStore === true`)
4216
4227
  try {
4217
4228
  this.cookieStore = await (0, headers_1.cookies)();
@@ -4222,7 +4233,7 @@ class VtecxNext {
4222
4233
  }
4223
4234
  this.shouldBeCreatedCookieStore = false;
4224
4235
  }
4225
- if (this.cookieStore) {
4236
+ if (this.useCookieStore && this.cookieStore) {
4226
4237
  //console.log(`[editRequestCookie] this.cookieStore === true. ${JSON.stringify(this.cookieStore)}`)
4227
4238
  // server action用
4228
4239
  const cookieArray = this.cookieStore.getAll();
@@ -4901,6 +4912,40 @@ const createURLSearchParams = (data) => {
4901
4912
  Object.keys(data).forEach((key) => params.append(key, data[key]));
4902
4913
  return params;
4903
4914
  };
4915
+ /**
4916
+ * set-cookie header から Cookie 名と値だけを抽出する。
4917
+ * @param setCookieVal set-cookie header value
4918
+ * @returns cookie pair list
4919
+ */
4920
+ const splitSetCookieHeader = (setCookieVal) => {
4921
+ const cookies = [];
4922
+ let current = '';
4923
+ let inExpires = false;
4924
+ for (let i = 0; i < setCookieVal.length; i++) {
4925
+ const char = setCookieVal[i];
4926
+ if (!inExpires && setCookieVal.substring(i, i + 8).toLowerCase() === 'expires=') {
4927
+ inExpires = true;
4928
+ }
4929
+ if (char === ',' && !inExpires) {
4930
+ if (current) {
4931
+ cookies.push(current);
4932
+ }
4933
+ current = '';
4934
+ continue;
4935
+ }
4936
+ current += char;
4937
+ if (inExpires && char === ';') {
4938
+ inExpires = false;
4939
+ }
4940
+ }
4941
+ if (current) {
4942
+ cookies.push(current);
4943
+ }
4944
+ return cookies.map((cookie) => {
4945
+ const firstPart = cookie.split(';')[0];
4946
+ return firstPart.trim();
4947
+ }).filter((cookie) => cookie.length > 0);
4948
+ };
4904
4949
  /**
4905
4950
  * URIのパラメータ部分を連想配列にして返却
4906
4951
  * @param uri URI
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtecx/vtecxnext",
3
- "version": "3.0.6",
3
+ "version": "3.1.0",
4
4
  "description": "vte.cx Next.js api",
5
5
  "main": "dist/index.js",
6
6
  "files": [