@motionpicture/coa-service 9.5.0 → 9.6.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.
@@ -3,27 +3,98 @@
3
3
  */
4
4
  const COA = require('../');
5
5
  const fs = require('fs');
6
+ const redis = require('redis');
6
7
 
7
- const service = new COA.service.Master(
8
- {
9
- endpoint: process.env.COA_ENDPOINT,
10
- auth: new COA.auth.RefreshToken({
11
- endpoint: process.env.COA_ENDPOINT,
12
- refreshToken: process.env.COA_REFRESH_TOKEN,
13
- useFetch: true
14
- })
8
+ /**
9
+ * 認証情報リポジトリ
10
+ */
11
+ class CredentialsRepo {
12
+ redisClient;
13
+ options;
14
+
15
+ constructor(redisClient, options) {
16
+ this.redisClient = redisClient;
17
+ this.options = options;
18
+ }
19
+
20
+ async save(credentials) {
21
+ const { access_token, expired_at } = credentials;
22
+ if (typeof credentials.access_token !== 'string') {
23
+ throw new Error('access_token must be string');
24
+ }
25
+ if (typeof credentials.expired_at !== 'string') {
26
+ throw new Error('expired_at must be string');
27
+ }
28
+
29
+ const key = this.createKey();
30
+ const value = JSON.stringify({ access_token, expired_at });
31
+ const multi = this.redisClient.multi();
32
+ const results = await multi.set(key, value)
33
+ // .expireAt(key, Number(expired_at))
34
+ .expire(key, 60)
35
+ .exec();
36
+ console.log('credential saved', results);
37
+ // if (Array.isArray(results) && (results[0] === 1 || (<any>results)[0] === true)) {
38
+ // return true;
39
+ // } else {
40
+ // throw new Error('unexpected');
41
+ // }
42
+ }
43
+
44
+ async find() {
45
+ const key = this.createKey();
46
+
47
+ let credentials;
48
+ try {
49
+ const credentialsStr = await this.redisClient.get(key);
50
+ if (typeof credentialsStr === 'string') {
51
+ credentials = JSON.parse(credentialsStr);
52
+ }
53
+ } catch (error) {
54
+ console.error('credential parse error:', error);
55
+ }
56
+
57
+ return credentials;
15
58
  }
16
- );
17
-
18
- // const kubunClass = '000'; // すべて
19
- // const kubunClass = '050'; // 特別席区分
20
- const kubunClass = '005'; // 販売区分
21
- service.kubunName({
22
- theaterCode: '120',
23
- kubunClass
24
- }).then((result) => {
25
- fs.writeFileSync(`${__dirname}/output/kubunName-${kubunClass}.json`, JSON.stringify(result, null, ' '));
26
- console.log(result);
27
- }).catch((err) => {
28
- console.error(err);
59
+
60
+ createKey() {
61
+ return `COA:credentials:${this.options.scope}`;
62
+ }
63
+ }
64
+
65
+ const redisClient = redis.createClient({
66
+ socket: {
67
+ port: Number(process.env.REDIS_PORT),
68
+ host: process.env.REDIS_HOST
69
+ },
70
+ password: process.env.REDIS_KEY
29
71
  });
72
+ redisClient.connect()
73
+ .then(() => {
74
+ const credentialsRepo = new CredentialsRepo(redisClient, { scope: 'xxx' });
75
+ const service = new COA.service.Master(
76
+ {
77
+ endpoint: process.env.COA_ENDPOINT,
78
+ auth: new COA.auth.RefreshToken({
79
+ endpoint: process.env.COA_ENDPOINT,
80
+ refreshToken: process.env.COA_REFRESH_TOKEN,
81
+ useFetch: true,
82
+ credentialsRepo
83
+ })
84
+ }
85
+ );
86
+
87
+ // const kubunClass = '000'; // すべて
88
+ // const kubunClass = '050'; // 特別席区分
89
+ const kubunClass = '045'; // 販売区分
90
+ service.kubunName({
91
+ theaterCode: '120',
92
+ kubunClass
93
+ }).then((result) => {
94
+ fs.writeFileSync(`${__dirname}/output/kubunName-${kubunClass}.json`, JSON.stringify(result, null, ' '));
95
+ // console.log(result);
96
+ console.log('result saved.');
97
+ }).catch((err) => {
98
+ console.error(err);
99
+ });
100
+ });
@@ -8,6 +8,7 @@ export interface ICredentials {
8
8
  access_token?: string;
9
9
  /**
10
10
  * 期限UNIXタイムスタンプ
11
+ * @example 2024-11-20 08:28:47
11
12
  */
12
13
  expired_at?: string;
13
14
  }
@@ -1,9 +1,14 @@
1
1
  import { IRequestOptions } from '../transporters';
2
2
  import { ICredentials } from './credentials';
3
+ import { AbstractCredentialsRepo } from './repo/credentials';
3
4
  export interface IOptions {
4
5
  endpoint: string;
5
6
  refreshToken?: string;
6
7
  useFetch: boolean;
8
+ /**
9
+ * カスタム認証情報リポジトリ
10
+ */
11
+ credentialsRepo?: AbstractCredentialsRepo;
7
12
  }
8
13
  export { IRequestOptions };
9
14
  /**
@@ -93,6 +93,13 @@ class RefreshTokenClient {
93
93
  }
94
94
  throw err;
95
95
  }
96
+ // save in remote
97
+ // tslint:disable-next-line:no-single-line-block-comment
98
+ /* istanbul ignore next */
99
+ if (this.options.credentialsRepo !== undefined) {
100
+ debug('saving in repo...', this.credentials);
101
+ yield this.options.credentialsRepo.save(this.credentials);
102
+ }
96
103
  return this.credentials;
97
104
  });
98
105
  }
@@ -119,6 +126,19 @@ class RefreshTokenClient {
119
126
  */
120
127
  getAccessToken() {
121
128
  return __awaiter(this, void 0, void 0, function* () {
129
+ // find from remote
130
+ // localに存在せず、remoteに存在すれば採用
131
+ // tslint:disable-next-line:no-single-line-block-comment
132
+ /* istanbul ignore next */
133
+ if (typeof this.credentials.access_token !== 'string') {
134
+ if (this.options.credentialsRepo !== undefined) {
135
+ const credentialsFromRepo = yield this.options.credentialsRepo.find();
136
+ debug('credentials in repo found,', credentialsFromRepo);
137
+ if (typeof (credentialsFromRepo === null || credentialsFromRepo === void 0 ? void 0 : credentialsFromRepo.access_token) === 'string') {
138
+ this.credentials = credentialsFromRepo;
139
+ }
140
+ }
141
+ }
122
142
  const expiredAt = this.credentials.expired_at;
123
143
  const spareTimeInMilliseconds = RefreshTokenClient.DEFAULT_SPARE_TIME_IN_MILLISECONDS;
124
144
  let isTokenExpired = true;
@@ -0,0 +1,10 @@
1
+ import type { ICredentials } from '../credentials';
2
+ export type ISaveParams = ICredentials;
3
+ export type IFindResult = ICredentials | undefined;
4
+ /**
5
+ * 抽象認証情報リポジトリ
6
+ */
7
+ export declare abstract class AbstractCredentialsRepo {
8
+ abstract save(params: ISaveParams): Promise<void>;
9
+ abstract find(): Promise<IFindResult>;
10
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractCredentialsRepo = void 0;
4
+ /**
5
+ * 抽象認証情報リポジトリ
6
+ */
7
+ class AbstractCredentialsRepo {
8
+ }
9
+ exports.AbstractCredentialsRepo = AbstractCredentialsRepo;
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import * as factory from './factory';
5
5
  import { RefreshTokenClient } from './auth/refreshTokenClient';
6
+ import * as CredentialsRepo from './auth/repo/credentials';
6
7
  import { MasterService } from './service/master';
7
8
  import { ReserveService } from './service/reserve';
8
9
  import { COAServiceError } from './transporter/coaServiceError';
@@ -13,6 +14,9 @@ export declare namespace auth {
13
14
  */
14
15
  class RefreshToken extends RefreshTokenClient {
15
16
  }
17
+ namespace repo {
18
+ export import credentials = CredentialsRepo;
19
+ }
16
20
  }
17
21
  export declare namespace service {
18
22
  /**
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ exports.service = exports.auth = exports.factory = exports.COAServiceError = voi
7
7
  const factory = require("./factory");
8
8
  exports.factory = factory;
9
9
  const refreshTokenClient_1 = require("./auth/refreshTokenClient");
10
+ const CredentialsRepo = require("./auth/repo/credentials");
10
11
  const master_1 = require("./service/master");
11
12
  const reserve_1 = require("./service/reserve");
12
13
  const coaServiceError_1 = require("./transporter/coaServiceError");
@@ -19,6 +20,10 @@ var auth;
19
20
  class RefreshToken extends refreshTokenClient_1.RefreshTokenClient {
20
21
  }
21
22
  auth.RefreshToken = RefreshToken;
23
+ let repo;
24
+ (function (repo) {
25
+ repo.credentials = CredentialsRepo;
26
+ })(repo = auth.repo || (auth.repo = {}));
22
27
  })(auth = exports.auth || (exports.auth = {}));
23
28
  var service;
24
29
  (function (service) {
package/package.json CHANGED
@@ -30,6 +30,7 @@
30
30
  "mocha": "^5.2.0",
31
31
  "moment": "^2.22.1",
32
32
  "nyc": "^15.1.0",
33
+ "redis": "4.6.5",
33
34
  "rimraf": "^2.6.2",
34
35
  "sinon": "^5.0.10",
35
36
  "ts-node": "10.8.0",
@@ -76,5 +77,5 @@
76
77
  "postversion": "git push origin --tags",
77
78
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
78
79
  },
79
- "version": "9.5.0"
80
+ "version": "9.6.0"
80
81
  }