@vtecx/vtecxnext 2.2.1 → 2.2.2

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.
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { NextRequest } from 'next/server';
3
2
  import type { Readable } from 'node:stream';
4
3
  /**
@@ -27,18 +26,21 @@ export type CreateGroupadminInfo = {
27
26
  };
28
27
  export declare class VtecxNext {
29
28
  /** Request */
30
- readonly req: NextRequest;
29
+ readonly req: NextRequest | undefined;
31
30
  /** Response status */
32
31
  private resStatus;
33
32
  /** Response headers */
34
33
  private resHeaders;
35
34
  /** binary data */
36
35
  private bufferData;
36
+ /** Access Token (for batch) */
37
+ private accessToken;
37
38
  /**
38
39
  * constructor
39
40
  * @param req Request
41
+ * @param accessToken Access token (for batch)
40
42
  */
41
- constructor(req: NextRequest);
43
+ constructor(req?: NextRequest, accessToken?: string);
42
44
  /**
43
45
  * get url parameter.
44
46
  * @param name parameter name
@@ -773,14 +775,16 @@ export declare class VtecxNext {
773
775
  * @param uri key
774
776
  * @param bysize true if registering with specified size
775
777
  * @param filename attachment file name
778
+ * @param arrayBuffer content (for batch)
776
779
  * @return message
777
780
  */
778
- putcontent: (uri: string, filename?: string) => Promise<any>;
781
+ putcontent: (uri: string, filename?: string, arrayBuffer?: ArrayBuffer) => Promise<any>;
779
782
  /**
780
783
  * upload content
781
784
  * @param uri key
782
785
  * @param bysize true if registering with specified size
783
786
  * @param filename attachment file name
787
+ * @param arrayBuffer content (for batch)
784
788
  * @return message
785
789
  */
786
790
  private putcontentProc;
package/dist/vtecxnext.js CHANGED
@@ -54,12 +54,21 @@ class VtecxNext {
54
54
  resHeaders = {};
55
55
  /** binary data */
56
56
  bufferData = null;
57
+ /** Access Token (for batch) */
58
+ accessToken;
57
59
  /**
58
60
  * constructor
59
61
  * @param req Request
62
+ * @param accessToken Access token (for batch)
60
63
  */
61
- constructor(req) {
62
- this.req = req;
64
+ constructor(req, accessToken) {
65
+ if (req) {
66
+ this.req = req;
67
+ }
68
+ else {
69
+ this.req = undefined;
70
+ this.accessToken = accessToken;
71
+ }
63
72
  }
64
73
  /**
65
74
  * get url parameter.
@@ -67,6 +76,9 @@ class VtecxNext {
67
76
  * @returns parameter value
68
77
  */
69
78
  getParameter = (name) => {
79
+ if (!this.req) {
80
+ throw new VtecxNextError(421, 'Request is required.');
81
+ }
70
82
  const url = new URL(this.req.url);
71
83
  const params = url.searchParams;
72
84
  const val = params.get(name);
@@ -91,6 +103,9 @@ class VtecxNext {
91
103
  * @returns buffer
92
104
  */
93
105
  buffer = async (readable) => {
106
+ if (!this.req) {
107
+ throw new VtecxNextError(421, 'Request is required.');
108
+ }
94
109
  let tmpReadable;
95
110
  if (readable === undefined || readable === null) {
96
111
  const arrayBuffer = await this.req.arrayBuffer();
@@ -127,6 +142,9 @@ class VtecxNext {
127
142
  */
128
143
  checkXRequestedWith = () => {
129
144
  //console.log(`[vtecxnext checkXRequestedWith] start.`)
145
+ if (!this.req) {
146
+ throw new VtecxNextError(421, 'Request is required.');
147
+ }
130
148
  let hasX = false;
131
149
  this.req.headers.forEach((value, key, parent) => {
132
150
  //console.log(`[vtecxnext checkXRequestedWith] key=${key} value=${value}`)
@@ -3039,6 +3057,9 @@ class VtecxNext {
3039
3057
  */
3040
3058
  savefiles = async (uri, bysize) => {
3041
3059
  //console.log(`[vtecxnext savefiles] start. uri=${uri}`)
3060
+ if (!this.req) {
3061
+ throw new VtecxNextError(421, 'Request is required.');
3062
+ }
3042
3063
  // キー入力値チェック
3043
3064
  checkUri(uri);
3044
3065
  const formData = await this.req.formData();
@@ -3092,32 +3113,43 @@ class VtecxNext {
3092
3113
  * @param uri key
3093
3114
  * @param bysize true if registering with specified size
3094
3115
  * @param filename attachment file name
3116
+ * @param arrayBuffer content (for batch)
3095
3117
  * @return message
3096
3118
  */
3097
- putcontent = async (uri, filename) => {
3098
- return this.putcontentProc(uri, false, filename);
3119
+ putcontent = async (uri, filename, arrayBuffer) => {
3120
+ return this.putcontentProc(uri, false, filename, arrayBuffer);
3099
3121
  };
3100
3122
  /**
3101
3123
  * upload content
3102
3124
  * @param uri key
3103
3125
  * @param bysize true if registering with specified size
3104
3126
  * @param filename attachment file name
3127
+ * @param arrayBuffer content (for batch)
3105
3128
  * @return message
3106
3129
  */
3107
- putcontentProc = async (uri, bysize, filename) => {
3130
+ putcontentProc = async (uri, bysize, filename, arrayBuffer) => {
3108
3131
  //console.log(`[vtecxnext putcontent] start. uri=${uri} content-type:${req.headers['content-type']} content-length:${req.headers['content-length']}`)
3132
+ if (!this.req && !arrayBuffer) {
3133
+ throw new VtecxNextError(421, 'Request is required.');
3134
+ }
3109
3135
  // キー入力値チェック
3110
3136
  checkUri(uri);
3111
3137
  // vte.cxへリクエスト
3112
3138
  const method = 'PUT';
3113
3139
  const url = `${SERVLETPATH_PROVIDER}${uri}?_content${bysize ? '&_bysize' : ''}`;
3114
3140
  //console.log(`[vtecxnext putcontent] request. url=${url}`)
3115
- const headers = { 'Content-Type': this.req.headers.get('content-type') };
3141
+ const headers = { 'Content-Type': this.req?.headers.get('content-type') };
3116
3142
  if (filename) {
3117
3143
  headers['Content-Disposition'] = `attachment; filename="${encodeURIComponent(filename)}"`;
3118
3144
  }
3119
3145
  //const buf = await buffer(this.req)
3120
- const buf = await this.req.arrayBuffer();
3146
+ let buf;
3147
+ if (arrayBuffer) {
3148
+ buf = arrayBuffer;
3149
+ }
3150
+ else if (this.req) {
3151
+ buf = await this.req.arrayBuffer();
3152
+ }
3121
3153
  let response;
3122
3154
  try {
3123
3155
  response = await this.requestVtecx(method, url, buf, headers);
@@ -3149,6 +3181,9 @@ class VtecxNext {
3149
3181
  */
3150
3182
  postcontent = async (parenturi, extension, filename) => {
3151
3183
  //console.log(`[vtecxnext postcontent] start. parenturi=${parenturi} extension=${extension} filename=${filename}`)
3184
+ if (!this.req) {
3185
+ throw new VtecxNextError(421, 'Request is required.');
3186
+ }
3152
3187
  // キー入力値チェック
3153
3188
  checkUri(parenturi);
3154
3189
  // vte.cxへリクエスト
@@ -3256,7 +3291,7 @@ class VtecxNext {
3256
3291
  const method = 'PUT';
3257
3292
  const url = `${SERVLETPATH_PROVIDER}${uri}?_content&_signedurl`;
3258
3293
  //console.log(`[vtecxnext getSignedUrlToPutContent] request. url=${url}`)
3259
- const headers = { 'Content-Type': this.req.headers.get('content-type') };
3294
+ const headers = { 'Content-Type': this.req?.headers?.get('content-type') };
3260
3295
  if (filename) {
3261
3296
  headers['Content-Disposition'] = `attachment; filename="${encodeURIComponent(filename)}"`;
3262
3297
  }
@@ -3288,7 +3323,7 @@ class VtecxNext {
3288
3323
  const method = 'POST';
3289
3324
  const url = `${SERVLETPATH_PROVIDER}${parenturi}?_content&_signedurl${extension ? '&_ext=' + extension : ''}`;
3290
3325
  //console.log(`[vtecxnext getSignedUrlToPostContent] request. url=${url}`)
3291
- const headers = { 'Content-Type': this.req.headers.get('content-type') };
3326
+ const headers = { 'Content-Type': this.req?.headers?.get('content-type') };
3292
3327
  if (filename) {
3293
3328
  headers['Content-Disposition'] = `attachment; filename="${encodeURIComponent(filename)}"`;
3294
3329
  }
@@ -3556,6 +3591,9 @@ class VtecxNext {
3556
3591
  // cookieの値をvte.cxへのリクエストヘッダに設定
3557
3592
  const cookie = this.req ? this.req.headers.get('cookie') : undefined;
3558
3593
  const headers = cookie ? { 'Cookie': cookie } : {};
3594
+ if (this.accessToken) {
3595
+ headers.Authorization = `Token ${this.accessToken}`;
3596
+ }
3559
3597
  if (additionalHeaders) {
3560
3598
  //console.log(`[vtecxnext requestVtecx] additionalHeaders for`)
3561
3599
  for (const key in additionalHeaders) {
@@ -3623,6 +3661,9 @@ class VtecxNext {
3623
3661
  */
3624
3662
  oauth = async (provider, oauthUrl) => {
3625
3663
  //console.log(`[vtecxnext oauth] start. provider=${provider} oauthUrl=${oauthUrl}`)
3664
+ if (!this.req) {
3665
+ throw new VtecxNextError(421, 'Request is required.');
3666
+ }
3626
3667
  // TODO reCAPTCHAを必須とすべき?
3627
3668
  // 入力チェック
3628
3669
  checkNotNull(provider, 'OAuth provider');
@@ -3672,6 +3713,9 @@ class VtecxNext {
3672
3713
  */
3673
3714
  oauthGetAccesstoken = async (provider, accesstokenUrl) => {
3674
3715
  //console.log(`[vtecxnext oauthGetAccesstoken] start. provider=${provider} oauthUrl=${accesstokenUrl}`)
3716
+ if (!this.req) {
3717
+ throw new VtecxNextError(421, 'Request is required.');
3718
+ }
3675
3719
  // stateチェック
3676
3720
  const parseUrl = url_1.default.parse(this.req.url ?? '', true);
3677
3721
  const state = parseUrl.query.state;
@@ -3721,12 +3765,16 @@ class VtecxNext {
3721
3765
  'client_id': client_id,
3722
3766
  'client_secret': client_secret
3723
3767
  };
3724
- const accesstokenBody = createURLSearchParams(accessTokenData);
3768
+ const accesstokenBody = createURLSearchParams(accessTokenData).toString();
3725
3769
  //const accesstokenBodyStr = `grant_type=authorization_code&code=${code}&redirect_uri=${encodeRedirect_uri}&client_id=${client_id}&client_secret=${client_secret}`
3726
3770
  //console.log(`[vtecxnext oauthGetAccesstoken] accesstokenUrl=${accesstokenUrl}`)
3727
3771
  //console.log(`[vtecxnext oauthGetAccesstoken] accesstokenBodyStr=${accesstokenBodyStr}`)
3728
3772
  //const accesstokenBody = Buffer.from(accesstokenBodyStr, 'utf-8')
3729
3773
  const requestInit = {
3774
+ headers: {
3775
+ 'Content-Type': 'application/x-www-form-urlencoded',
3776
+ 'Content-Length': String(accesstokenBody.length),
3777
+ },
3730
3778
  body: accesstokenBody,
3731
3779
  method: accesstokenMethod,
3732
3780
  cache: 'no-cache',
@@ -3909,33 +3957,27 @@ exports.FetchError = FetchError;
3909
3957
  * vte.cxへリクエスト
3910
3958
  * @param method メソッド
3911
3959
  * @param url サーブレットパス以降のURL
3912
- * @param headers リクエストヘッダ。連想配列で指定。
3960
+ * @param pHeaders リクエストヘッダ。連想配列で指定。
3913
3961
  * @param body リクエストデータ
3914
3962
  * @param mode RequestMode ("cors" | "navigate" | "no-cors" | "same-origin")
3915
3963
  * @returns promise
3916
3964
  */
3917
- const fetchVtecx = async (method, url, headers, body, mode) => {
3965
+ const fetchVtecx = async (method, url, pHeaders, body, mode) => {
3918
3966
  //console.log(`[vtecxnext fetchVtecx] url=${process.env.VTECX_URL}${url}`)
3919
- headers['X-Requested-With'] = 'XMLHttpRequest';
3967
+ const headers = [];
3968
+ if (pHeaders) {
3969
+ for (const key in pHeaders) {
3970
+ headers.push([key, pHeaders[key]]);
3971
+ }
3972
+ }
3973
+ headers.push(['X-Requested-With', 'XMLHttpRequest']);
3920
3974
  if (VTECX_SERVICENAME) {
3921
- headers['X-SERVICENAME'] = VTECX_SERVICENAME;
3975
+ headers.push(['X-SERVICENAME', VTECX_SERVICENAME]);
3922
3976
  }
3923
3977
  const apiKey = process.env.VTECX_APIKEY;
3924
3978
  if (apiKey && !url.startsWith(SERVLETPATH_DATA)) {
3925
- //headers['Authorization'] = `APIKey ${apiKey}`
3926
3979
  const apiKeyVal = `APIKey ${apiKey}`;
3927
- if (headers.Authorization) {
3928
- if (Array.isArray(headers.Authorization)) {
3929
- headers.Authorization.push(apiKeyVal);
3930
- }
3931
- else {
3932
- const tmp = headers.Authorization;
3933
- headers.Authorization = [tmp, apiKeyVal];
3934
- }
3935
- }
3936
- else {
3937
- headers.Authorization = apiKeyVal;
3938
- }
3980
+ headers.push(['Authorization', apiKeyVal]);
3939
3981
  }
3940
3982
  //console.log(`[vtecxnext fetchVtecx] headers = ${JSON.stringify(headers)}`)
3941
3983
  const requestInit = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtecx/vtecxnext",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "description": "vte.cx Next.js api",
5
5
  "main": "dist/index.js",
6
6
  "files": [