@vtecx/vtecxnext 2.2.0 → 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;
@@ -876,6 +880,13 @@ export declare class VtecxNext {
876
880
  * @return message feed
877
881
  */
878
882
  createGroupadmin: (createGroupadminInfos: CreateGroupadminInfo[]) => Promise<any>;
883
+ /**
884
+ * delete group admin group
885
+ * @param groupNames group name list
886
+ * @param async execute async
887
+ * @return message feed
888
+ */
889
+ deleteGroupadmin: (groupNames: string[], async?: boolean) => Promise<any>;
879
890
  /**
880
891
  * vte.cxへリクエスト
881
892
  * @param method メソッド
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
  }
@@ -3505,6 +3540,41 @@ class VtecxNext {
3505
3540
  await checkVtecxResponse(response);
3506
3541
  return await getJson(response);
3507
3542
  };
3543
+ /**
3544
+ * delete group admin group
3545
+ * @param groupNames group name list
3546
+ * @param async execute async
3547
+ * @return message feed
3548
+ */
3549
+ deleteGroupadmin = async (groupNames, async) => {
3550
+ //console.log(`[vtecxnext deleteGroupadmin] start. feed=${feed}`)
3551
+ // 入力チェック
3552
+ checkNotNull(groupNames, 'group name');
3553
+ const feed = [];
3554
+ for (const groupName of groupNames) {
3555
+ checkNotNull(groupName, 'group name');
3556
+ checkContainSlash(groupName, 'group name');
3557
+ const links = [{ '___rel': 'self', '___href': `/_group/${groupName}` }];
3558
+ const entry = { 'link': links };
3559
+ feed.push(entry);
3560
+ }
3561
+ // vte.cxへリクエスト
3562
+ const method = 'DELETE';
3563
+ const url = `${SERVLETPATH_DATA}/?_deletegroupadmin${async ? '&_async' : ''}`;
3564
+ let response;
3565
+ try {
3566
+ response = await this.requestVtecx(method, url, JSON.stringify(feed));
3567
+ }
3568
+ catch (e) {
3569
+ throw newFetchError(e, true);
3570
+ }
3571
+ //console.log(`[vtecxnext deleteGroupadmin] response. status=${response.status}`)
3572
+ // vte.cxからのset-cookieを転記
3573
+ this.setCookie(response);
3574
+ // レスポンスのエラーチェック
3575
+ await checkVtecxResponse(response);
3576
+ return await getJson(response);
3577
+ };
3508
3578
  //----------------------
3509
3579
  /**
3510
3580
  * vte.cxへリクエスト
@@ -3521,6 +3591,9 @@ class VtecxNext {
3521
3591
  // cookieの値をvte.cxへのリクエストヘッダに設定
3522
3592
  const cookie = this.req ? this.req.headers.get('cookie') : undefined;
3523
3593
  const headers = cookie ? { 'Cookie': cookie } : {};
3594
+ if (this.accessToken) {
3595
+ headers.Authorization = `Token ${this.accessToken}`;
3596
+ }
3524
3597
  if (additionalHeaders) {
3525
3598
  //console.log(`[vtecxnext requestVtecx] additionalHeaders for`)
3526
3599
  for (const key in additionalHeaders) {
@@ -3588,6 +3661,9 @@ class VtecxNext {
3588
3661
  */
3589
3662
  oauth = async (provider, oauthUrl) => {
3590
3663
  //console.log(`[vtecxnext oauth] start. provider=${provider} oauthUrl=${oauthUrl}`)
3664
+ if (!this.req) {
3665
+ throw new VtecxNextError(421, 'Request is required.');
3666
+ }
3591
3667
  // TODO reCAPTCHAを必須とすべき?
3592
3668
  // 入力チェック
3593
3669
  checkNotNull(provider, 'OAuth provider');
@@ -3637,6 +3713,9 @@ class VtecxNext {
3637
3713
  */
3638
3714
  oauthGetAccesstoken = async (provider, accesstokenUrl) => {
3639
3715
  //console.log(`[vtecxnext oauthGetAccesstoken] start. provider=${provider} oauthUrl=${accesstokenUrl}`)
3716
+ if (!this.req) {
3717
+ throw new VtecxNextError(421, 'Request is required.');
3718
+ }
3640
3719
  // stateチェック
3641
3720
  const parseUrl = url_1.default.parse(this.req.url ?? '', true);
3642
3721
  const state = parseUrl.query.state;
@@ -3686,12 +3765,16 @@ class VtecxNext {
3686
3765
  'client_id': client_id,
3687
3766
  'client_secret': client_secret
3688
3767
  };
3689
- const accesstokenBody = createURLSearchParams(accessTokenData);
3768
+ const accesstokenBody = createURLSearchParams(accessTokenData).toString();
3690
3769
  //const accesstokenBodyStr = `grant_type=authorization_code&code=${code}&redirect_uri=${encodeRedirect_uri}&client_id=${client_id}&client_secret=${client_secret}`
3691
3770
  //console.log(`[vtecxnext oauthGetAccesstoken] accesstokenUrl=${accesstokenUrl}`)
3692
3771
  //console.log(`[vtecxnext oauthGetAccesstoken] accesstokenBodyStr=${accesstokenBodyStr}`)
3693
3772
  //const accesstokenBody = Buffer.from(accesstokenBodyStr, 'utf-8')
3694
3773
  const requestInit = {
3774
+ headers: {
3775
+ 'Content-Type': 'application/x-www-form-urlencoded',
3776
+ 'Content-Length': String(accesstokenBody.length),
3777
+ },
3695
3778
  body: accesstokenBody,
3696
3779
  method: accesstokenMethod,
3697
3780
  cache: 'no-cache',
@@ -3874,33 +3957,27 @@ exports.FetchError = FetchError;
3874
3957
  * vte.cxへリクエスト
3875
3958
  * @param method メソッド
3876
3959
  * @param url サーブレットパス以降のURL
3877
- * @param headers リクエストヘッダ。連想配列で指定。
3960
+ * @param pHeaders リクエストヘッダ。連想配列で指定。
3878
3961
  * @param body リクエストデータ
3879
3962
  * @param mode RequestMode ("cors" | "navigate" | "no-cors" | "same-origin")
3880
3963
  * @returns promise
3881
3964
  */
3882
- const fetchVtecx = async (method, url, headers, body, mode) => {
3965
+ const fetchVtecx = async (method, url, pHeaders, body, mode) => {
3883
3966
  //console.log(`[vtecxnext fetchVtecx] url=${process.env.VTECX_URL}${url}`)
3884
- 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']);
3885
3974
  if (VTECX_SERVICENAME) {
3886
- headers['X-SERVICENAME'] = VTECX_SERVICENAME;
3975
+ headers.push(['X-SERVICENAME', VTECX_SERVICENAME]);
3887
3976
  }
3888
3977
  const apiKey = process.env.VTECX_APIKEY;
3889
3978
  if (apiKey && !url.startsWith(SERVLETPATH_DATA)) {
3890
- //headers['Authorization'] = `APIKey ${apiKey}`
3891
3979
  const apiKeyVal = `APIKey ${apiKey}`;
3892
- if (headers.Authorization) {
3893
- if (Array.isArray(headers.Authorization)) {
3894
- headers.Authorization.push(apiKeyVal);
3895
- }
3896
- else {
3897
- const tmp = headers.Authorization;
3898
- headers.Authorization = [tmp, apiKeyVal];
3899
- }
3900
- }
3901
- else {
3902
- headers.Authorization = apiKeyVal;
3903
- }
3980
+ headers.push(['Authorization', apiKeyVal]);
3904
3981
  }
3905
3982
  //console.log(`[vtecxnext fetchVtecx] headers = ${JSON.stringify(headers)}`)
3906
3983
  const requestInit = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtecx/vtecxnext",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "vte.cx Next.js api",
5
5
  "main": "dist/index.js",
6
6
  "files": [