@quesmed/types 1.3.17 → 1.3.18

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.
Files changed (83) hide show
  1. package/gql_input_output_types/User.cjs +2 -0
  2. package/gql_input_output_types/index.cjs +2 -0
  3. package/index.cjs +20 -0
  4. package/models/Author.cjs +2 -0
  5. package/models/Book.cjs +2 -0
  6. package/models/Card.cjs +2 -0
  7. package/models/Chapter.cjs +2 -0
  8. package/models/Concept.cjs +2 -0
  9. package/models/Difficulty.cjs +10 -0
  10. package/models/Feedback.cjs +2 -0
  11. package/models/File.cjs +2 -0
  12. package/models/Marksheet.cjs +9 -0
  13. package/models/MockTest.cjs +2 -0
  14. package/models/OsceMarksheet.cjs +23 -0
  15. package/models/OsceStation.cjs +45 -0
  16. package/models/Picture.cjs +6 -0
  17. package/models/Promo.cjs +2 -0
  18. package/models/Question.cjs +37 -0
  19. package/models/Subscription.cjs +9 -0
  20. package/models/Todo.cjs +2 -0
  21. package/models/Token.cjs +2 -0
  22. package/models/Topic.cjs +13 -0
  23. package/models/Type.cjs +2 -0
  24. package/models/University.cjs +2 -0
  25. package/models/User.cjs +2 -0
  26. package/models/Video.cjs +2 -0
  27. package/models/index.cjs +35 -0
  28. package/package.json +3 -2
  29. package/resolvers/apollo.cjs +2 -0
  30. package/resolvers/mutation/admin/algoliaSync.cjs +2 -0
  31. package/resolvers/mutation/admin/index.cjs +14 -0
  32. package/resolvers/mutation/admin/token.cjs +2 -0
  33. package/resolvers/mutation/index.cjs +14 -0
  34. package/resolvers/mutation/restricted/agora.cjs +2 -0
  35. package/resolvers/mutation/restricted/contactUs.cjs +2 -0
  36. package/resolvers/mutation/restricted/index.cjs +22 -0
  37. package/resolvers/mutation/restricted/marksheet.cjs +2 -0
  38. package/resolvers/mutation/restricted/mockTest.cjs +2 -0
  39. package/resolvers/mutation/restricted/osce.cjs +2 -0
  40. package/resolvers/mutation/restricted/questionDiscussion.cjs +2 -0
  41. package/resolvers/mutation/restricted/todo.cjs +29 -0
  42. package/resolvers/mutation/restricted/token.cjs +2 -0
  43. package/resolvers/mutation/restricted/users.cjs +2 -0
  44. package/resolvers/mutation/restricted/video.cjs +2 -0
  45. package/resolvers/mutation/stripe.cjs +2 -0
  46. package/resolvers/mutation/users.cjs +2 -0
  47. package/resolvers/mutation/validUserToken/index.cjs +13 -0
  48. package/resolvers/mutation/validUserToken/user.cjs +2 -0
  49. package/resolvers/query/admin/getUserToken.cjs +2 -0
  50. package/resolvers/query/admin/index.cjs +13 -0
  51. package/resolvers/query/author.cjs +2 -0
  52. package/resolvers/query/book.cjs +2 -0
  53. package/resolvers/query/feedback.cjs +2 -0
  54. package/resolvers/query/index.cjs +21 -0
  55. package/resolvers/query/restricted/anatomy.cjs +2 -0
  56. package/resolvers/query/restricted/index.cjs +22 -0
  57. package/resolvers/query/restricted/marksheet.cjs +2 -0
  58. package/resolvers/query/restricted/mockTests.cjs +2 -0
  59. package/resolvers/query/restricted/osce.cjs +2 -0
  60. package/resolvers/query/restricted/quesBook.cjs +2 -0
  61. package/resolvers/query/restricted/todos.cjs +2 -0
  62. package/resolvers/query/restricted/topics.cjs +2 -0
  63. package/resolvers/query/restricted/university.cjs +2 -0
  64. package/resolvers/query/restricted/user.cjs +2 -0
  65. package/resolvers/query/restricted/video.cjs +2 -0
  66. package/resolvers/query/sampleCards.cjs +2 -0
  67. package/resolvers/query/sampleQuestions.cjs +2 -0
  68. package/resolvers/query/subscription.cjs +2 -0
  69. package/resolvers/query/university.cjs +2 -0
  70. package/resolvers/query/user.cjs +2 -0
  71. package/resolvers/query/video.cjs +2 -0
  72. package/resolvers/subscription/index.cjs +13 -0
  73. package/resolvers/subscription/osce.cjs +43 -0
  74. package/utils/commonFunctions.cjs +223 -0
  75. package/utils/index.cjs +17 -0
  76. package/utils/index.d.ts +5 -0
  77. package/utils/index.js +5 -0
  78. package/utils/lightgallery.cjs +91 -0
  79. package/utils/offlineLink.cjs +237 -0
  80. package/{resolvers → utils}/offlineLink.d.ts +0 -0
  81. package/{resolvers → utils}/offlineLink.js +1 -1
  82. package/utils/uuid4.cjs +287 -0
  83. package/utils/wordsToNumber.cjs +48 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./osce"), exports);
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OsceGroupInit = exports.OSCE_MATCHMAKING_USERS = exports.OSCE_MATCHMAKING = exports.OSCE_MARKSHEET_ACTION = exports.ROLE_CHANGED = void 0;
4
+ const models_1 = require("../../models");
5
+ exports.ROLE_CHANGED = 'ROLE_CHANGED';
6
+ exports.OSCE_MARKSHEET_ACTION = 'OSCE_MARKSHEET_ACTION';
7
+ exports.OSCE_MATCHMAKING = 'OSCE_MATCHMAKING';
8
+ exports.OSCE_MATCHMAKING_USERS = 'OSCE_MATCHMAKING_USERS';
9
+ const OsceGroupInit = (osceMarksheetId) => [
10
+ {
11
+ id: null,
12
+ createdAt: null,
13
+ osceMarksheetId,
14
+ userId: null,
15
+ user: null,
16
+ role: models_1.EOsceRoles.CANDIDATE,
17
+ },
18
+ {
19
+ id: null,
20
+ createdAt: null,
21
+ osceMarksheetId,
22
+ userId: null,
23
+ user: null,
24
+ role: models_1.EOsceRoles.EXAMINER,
25
+ },
26
+ {
27
+ id: null,
28
+ createdAt: null,
29
+ osceMarksheetId,
30
+ userId: null,
31
+ user: null,
32
+ role: models_1.EOsceRoles.ACTOR,
33
+ },
34
+ {
35
+ id: null,
36
+ createdAt: null,
37
+ osceMarksheetId,
38
+ userId: null,
39
+ user: null,
40
+ role: models_1.EOsceRoles.OBSERVER,
41
+ },
42
+ ];
43
+ exports.OsceGroupInit = OsceGroupInit;
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.correctMark = exports.formatPrescribeAnswer = exports.mapPrescribeMarkToAnswer = void 0;
4
+ const models_1 = require("../models");
5
+ const wordsToNumber_1 = require("./wordsToNumber");
6
+ const floatRegex = /([0-9]*[.])?[0-9]+/gm;
7
+ const ACCEPTED_FREQ = ['stat', 'od', 'bd', 'tds', 'qds'];
8
+ function mapPrescribeMarkToAnswer(obj) {
9
+ return {
10
+ drug: {
11
+ value: obj.drug,
12
+ display: false,
13
+ },
14
+ dose: {
15
+ value: parseFloat(obj.dose),
16
+ display: false,
17
+ },
18
+ units: {
19
+ value: obj.units,
20
+ display: false,
21
+ },
22
+ route: {
23
+ value: obj.route,
24
+ display: false,
25
+ },
26
+ frequency: {
27
+ value: obj.frequency,
28
+ display: false,
29
+ },
30
+ duration: {
31
+ value: obj.duration,
32
+ display: false,
33
+ },
34
+ };
35
+ }
36
+ exports.mapPrescribeMarkToAnswer = mapPrescribeMarkToAnswer;
37
+ function formatPrescribeAnswer(obj) {
38
+ obj.drug.value = obj.drug.value.toLowerCase().replace(/\s/g, '');
39
+ obj.route.value = obj.route.value.toLowerCase().replace(/\s/g, '');
40
+ if (obj.route.value === 'oral') {
41
+ obj.route.value = 'po';
42
+ }
43
+ else if (obj.route.value.includes('intravenous')) {
44
+ obj.route.value = 'iv';
45
+ }
46
+ else if (obj.route.value.includes('intramuscular')) {
47
+ obj.route.value = 'im';
48
+ }
49
+ else if (obj.route.value.includes('subcutaneous')) {
50
+ obj.route.value = 'sc';
51
+ }
52
+ else if (obj.route.value.includes('inhale')) {
53
+ obj.route.value = 'inh';
54
+ }
55
+ else if (obj.route.value.includes('nebuli')) {
56
+ obj.route.value = 'neb';
57
+ }
58
+ else if (obj.route.value.includes('topical')) {
59
+ obj.route.value = 'top';
60
+ }
61
+ obj.units.value = obj.units.value.toLowerCase().replace(/\s/g, '');
62
+ if (obj.units.value === 'milligram' || obj.units.value === 'milligrams') {
63
+ obj.units.value = 'mg';
64
+ }
65
+ else if (obj.units.value === 'microgram' || obj.units.value === 'micrograms') {
66
+ obj.units.value = 'mcg';
67
+ }
68
+ else if (obj.units.value === 'gram' || obj.units.value === 'grams') {
69
+ obj.units.value = 'g';
70
+ }
71
+ else if (obj.units.value === 'unit') {
72
+ obj.units.value = 'units';
73
+ }
74
+ let m;
75
+ const durationMatches = new Set();
76
+ while ((m = floatRegex.exec(obj.duration.value)) !== null) {
77
+ if (m.index === floatRegex.lastIndex) {
78
+ floatRegex.lastIndex++;
79
+ }
80
+ m.forEach((match) => durationMatches.add(match));
81
+ }
82
+ const durationWords = (0, wordsToNumber_1.wordsToNumber)(obj.duration.value, false);
83
+ if (durationWords !== 0) {
84
+ durationMatches.add(durationWords.toString());
85
+ }
86
+ obj.duration.value = [...durationMatches].filter((x) => !!x).join(',');
87
+ const orgFrequencyValue = obj.frequency.value.toLowerCase();
88
+ obj.frequency.value = orgFrequencyValue.replace(/\s/g, '');
89
+ const frequencyIncHourly = obj.frequency.value.includes('hour');
90
+ const hourlyFreqInt = parseInt(obj.frequency.value);
91
+ if (obj.frequency.value === 'once-off') {
92
+ obj.frequency.value = 'stat';
93
+ }
94
+ else if (obj.frequency.value.includes('once') || (frequencyIncHourly && hourlyFreqInt === 24)) {
95
+ obj.frequency.value = 'od';
96
+ }
97
+ else if (obj.frequency.value.includes('nightly') || obj.frequency.value === 'on') {
98
+ obj.frequency.value = 'od';
99
+ }
100
+ else if (obj.frequency.value.includes('twice') ||
101
+ (frequencyIncHourly && hourlyFreqInt === 12)) {
102
+ obj.frequency.value = 'bd';
103
+ }
104
+ else if (obj.frequency.value.includes('trice') || (frequencyIncHourly && hourlyFreqInt === 8)) {
105
+ obj.frequency.value = 'tds';
106
+ }
107
+ else if (frequencyIncHourly && hourlyFreqInt === 6) {
108
+ obj.frequency.value = 'qds';
109
+ }
110
+ else if (!ACCEPTED_FREQ.includes(obj.frequency.value)) {
111
+ obj.frequency.value = (0, wordsToNumber_1.wordsToNumber)(orgFrequencyValue, true).toString();
112
+ }
113
+ return obj;
114
+ }
115
+ exports.formatPrescribeAnswer = formatPrescribeAnswer;
116
+ const answerRegex = /answer/gi;
117
+ function correctMark(mark) {
118
+ const data = {
119
+ correct: false,
120
+ incorrect: false,
121
+ correctIndex: -1,
122
+ };
123
+ if (!mark || !mark.mark) {
124
+ return data;
125
+ }
126
+ try {
127
+ const qKeys = Object.keys(mark.question);
128
+ const answerKey = qKeys.find((k) => answerRegex.exec(k)) || 'answer';
129
+ const answer = mark.question[answerKey];
130
+ const flatAnswer = answer[0];
131
+ const flatAttempt = mark.mark[0];
132
+ if (models_1.EQuestionType.SINGLE_BEST_ANSWER === mark.question.typeId) {
133
+ const answer = flatAnswer;
134
+ const attempt = flatAttempt;
135
+ if (answer === attempt) {
136
+ data.correct = true;
137
+ }
138
+ else {
139
+ data.incorrect = true;
140
+ }
141
+ }
142
+ else if (models_1.EQuestionType.QUESTION_ANSWER === mark.question.typeId) {
143
+ const answer = flatAnswer.dose.toLowerCase().replace(/\s/g, '');
144
+ let attempt = '';
145
+ if (flatAttempt) {
146
+ attempt = flatAttempt.toLowerCase().replace(/\s/g, '');
147
+ }
148
+ if (answer === attempt) {
149
+ data.correct = true;
150
+ }
151
+ else {
152
+ data.incorrect = true;
153
+ }
154
+ }
155
+ else if (models_1.EQuestionType.MULTIPLE_ANSWERS === mark.question.typeId) {
156
+ const [answerA, answerB] = answer.map((x) => x.sort());
157
+ let [attemptA, attemptB] = [[], []];
158
+ if (Array.isArray(mark.mark) &&
159
+ mark.mark.length === 2 &&
160
+ Array.isArray(mark.mark[0]) &&
161
+ Array.isArray(mark.mark[1])) {
162
+ [attemptA, attemptB] = mark.mark.map((x) => x.sort());
163
+ }
164
+ if (answerA.length !== attemptA.length || answerB.length !== attemptB.length) {
165
+ data.incorrect = true;
166
+ }
167
+ else if (attemptA.every((x, i) => x === answerA[i]) &&
168
+ attemptB.every((x, i) => x === answerB[i])) {
169
+ data.correct = true;
170
+ }
171
+ else {
172
+ data.incorrect = true;
173
+ }
174
+ }
175
+ else if (models_1.EQuestionType.PRESCRIPTION_ANSWER === mark.question.typeId) {
176
+ const answers = answer.map(formatPrescribeAnswer);
177
+ let attempt = formatPrescribeAnswer(mapPrescribeMarkToAnswer({
178
+ drug: '',
179
+ dose: -1,
180
+ units: '',
181
+ route: '',
182
+ frequency: '',
183
+ duration: '',
184
+ }));
185
+ if (typeof flatAttempt === 'object' && Object.keys(flatAttempt).length === 6) {
186
+ attempt = formatPrescribeAnswer(mapPrescribeMarkToAnswer(flatAttempt));
187
+ }
188
+ let foundCorrect = false;
189
+ let index = 0;
190
+ for (const answer of answers) {
191
+ const doseCorrect = answer.dose.display || answer.dose.value === attempt.dose.value;
192
+ const drugCorrect = answer.drug.display || answer.drug.value === attempt.drug.value;
193
+ const durationCorrect = answer.duration.display ||
194
+ answer.duration.value.split(',').includes(attempt.duration.value);
195
+ const frequencyCorrect = answer.frequency.display || answer.frequency.value === attempt.frequency.value;
196
+ const routeCorrect = answer.route.display || answer.route.value === attempt.route.value;
197
+ const unitsCorrect = answer.units.display || answer.units.value === attempt.units.value;
198
+ if (doseCorrect &&
199
+ drugCorrect &&
200
+ durationCorrect &&
201
+ frequencyCorrect &&
202
+ routeCorrect &&
203
+ unitsCorrect) {
204
+ foundCorrect = true;
205
+ break;
206
+ }
207
+ index++;
208
+ }
209
+ if (foundCorrect) {
210
+ data.correct = true;
211
+ data.correctIndex = index;
212
+ }
213
+ else {
214
+ data.incorrect = true;
215
+ }
216
+ }
217
+ }
218
+ catch (e) {
219
+ console.error(e);
220
+ }
221
+ return data;
222
+ }
223
+ exports.correctMark = correctMark;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./commonFunctions"), exports);
14
+ __exportStar(require("./lightgallery"), exports);
15
+ __exportStar(require("./offlineLink"), exports);
16
+ __exportStar(require("./uuid4"), exports);
17
+ __exportStar(require("./wordsToNumber"), exports);
@@ -0,0 +1,5 @@
1
+ export * from './commonFunctions';
2
+ export * from './lightgallery';
3
+ export * from './offlineLink';
4
+ export * from './uuid4';
5
+ export * from './wordsToNumber';
package/utils/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from './commonFunctions';
2
+ export * from './lightgallery';
3
+ export * from './offlineLink';
4
+ export * from './uuid4';
5
+ export * from './wordsToNumber';
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.lightgalleryOsceResolve = void 0;
13
+ const lightgalleryRegex = /\[lightgallery\]/;
14
+ function isOsceMarksheet(data) {
15
+ return 'osceStationId' in data;
16
+ }
17
+ function lightgalleryOsceResolve(data, cache) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ let station;
20
+ if (isOsceMarksheet(data)) {
21
+ if (!data.osceStation) {
22
+ throw new Error('OsceStation not found on OsceMarksheet');
23
+ }
24
+ station = data.osceStation;
25
+ }
26
+ else {
27
+ station = data;
28
+ }
29
+ const upOsceStation = {
30
+ candidateBrief: station.candidateBrief,
31
+ actorBrief: station.actorBrief,
32
+ examinerBrief: station.examinerBrief,
33
+ explanation: station.explanation,
34
+ };
35
+ if (station.candidatePictures && station.candidatePictures.length > 0) {
36
+ upOsceStation.candidateBrief = yield lightgalleryMutation(upOsceStation.candidateBrief, station.candidatePictures, cache);
37
+ }
38
+ if (station.actorPictures && station.actorPictures.length > 0) {
39
+ upOsceStation.actorBrief = yield lightgalleryMutation(upOsceStation.actorBrief, station.actorPictures, cache);
40
+ }
41
+ if (station.examinerPictures && station.examinerPictures.length > 0) {
42
+ upOsceStation.examinerBrief = yield lightgalleryMutation(upOsceStation.examinerBrief, station.examinerPictures, cache);
43
+ }
44
+ if (station.walkthroughPictures && station.walkthroughPictures.length > 0) {
45
+ upOsceStation.explanation = yield lightgalleryMutation(upOsceStation.explanation, station.walkthroughPictures, cache);
46
+ }
47
+ if (isOsceMarksheet(data)) {
48
+ return Object.assign(Object.assign({}, data), { osceStation: Object.assign(Object.assign({}, data.osceStation), upOsceStation) });
49
+ }
50
+ else {
51
+ return Object.assign(Object.assign({}, data), upOsceStation);
52
+ }
53
+ });
54
+ }
55
+ exports.lightgalleryOsceResolve = lightgalleryOsceResolve;
56
+ const lightgalleryMutation = (text, pictures, cache) => __awaiter(void 0, void 0, void 0, function* () {
57
+ var _a;
58
+ if (pictures.length === 0) {
59
+ return text;
60
+ }
61
+ const picturesHTML = [];
62
+ for (const { picture } of pictures) {
63
+ const pic = picture.path;
64
+ const uri = `https://app.quesmed.com/${pic}`;
65
+ let file = '';
66
+ if (cache) {
67
+ try {
68
+ const localPath = yield cache.CacheManager.get(uri, {}).getPath();
69
+ if (localPath) {
70
+ file =
71
+ cache.Platform.OS === 'ios'
72
+ ? localPath.replace('file://', '')
73
+ : localPath;
74
+ }
75
+ }
76
+ catch (e) {
77
+ console.error(e);
78
+ }
79
+ }
80
+ if (((_a = picture.caption) === null || _a === void 0 ? void 0 : _a.length) > 0) {
81
+ picturesHTML.push(`![${file}](${uri} "${picture.caption}")`);
82
+ }
83
+ picturesHTML.push(`![${file}](${uri})`);
84
+ }
85
+ if (lightgalleryRegex.test(text)) {
86
+ return text.replace(lightgalleryRegex, picturesHTML[0]);
87
+ }
88
+ else {
89
+ return text + '\n\n' + picturesHTML[0] + '\n';
90
+ }
91
+ });
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.OfflineLink = void 0;
16
+ const client_1 = require("@apollo/client");
17
+ const uuid4_1 = __importDefault(require("./uuid4"));
18
+ class OfflineLink extends client_1.ApolloLink {
19
+ /**
20
+ * storage
21
+ * Provider that will persist the mutation queue. This can be any AsyncStorage compatible storage instance.
22
+ *
23
+ * retryInterval
24
+ * Milliseconds between attempts to retry failed mutations. Defaults to 0, which disables automatic retry.
25
+ *
26
+ * sequential
27
+ * Indicates if the attempts should be retried in order. Defaults to false which retries all failed mutations in parallel.
28
+ *
29
+ * retryOnServerError
30
+ * Indicates if mutations should be reattempted if there are server side errors, useful to retry mutations on session expiration. Defaults to false.
31
+ */
32
+ constructor({ storage, retryInterval = 0, sequential = false, retryOnServerError = false, }) {
33
+ super();
34
+ this.queue = new Map();
35
+ this.prefix = 'offlineLink:';
36
+ if (!storage) {
37
+ throw new Error('Storage is required, it can be an AsyncStorage compatible storage instance.');
38
+ }
39
+ this.storage = storage;
40
+ this.sequential = sequential;
41
+ this.retryInterval = retryInterval;
42
+ this.retryOnServerError = retryOnServerError;
43
+ }
44
+ request(operation, forward) {
45
+ const context = operation.getContext();
46
+ const { query, variables } = operation || {};
47
+ if (!context.optimisticResponse) {
48
+ // If the mutation does not have an optimisticResponse then we can't defer it
49
+ return forward(operation);
50
+ }
51
+ return new client_1.Observable((observer) => {
52
+ const mutationId = uuid4_1.default.asBase64();
53
+ this.add({
54
+ mutation: query,
55
+ variables,
56
+ optimisticResponse: context.optimisticResponse,
57
+ }, mutationId);
58
+ const subscription = forward(operation).subscribe({
59
+ next: (result) => __awaiter(this, void 0, void 0, function* () {
60
+ yield this.remove(mutationId);
61
+ observer.next(result);
62
+ }),
63
+ error: () => {
64
+ // Mutation failed so we try again after a certain amount of time.
65
+ this.delayedSync();
66
+ // Resolve the mutation with the optimistic response so the UI can be updated
67
+ observer.next({
68
+ data: context.optimisticResponse,
69
+ dataPresent: true,
70
+ errors: [],
71
+ });
72
+ // Say we're all done so the UI is re-rendered.
73
+ observer.complete();
74
+ },
75
+ complete: () => observer.complete(),
76
+ });
77
+ return () => {
78
+ subscription.unsubscribe();
79
+ };
80
+ });
81
+ }
82
+ /**
83
+ * Obtains the queue of mutations that must be sent to the server.
84
+ * These are kept in a Map to preserve the order of the mutations in the queue.
85
+ */
86
+ getQueue() {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ const map = new Map();
89
+ try {
90
+ const storedMutations = yield this.storage.getItem(this.prefix + 'Mutations');
91
+ if (!storedMutations) {
92
+ return map;
93
+ }
94
+ const mutationIds = storedMutations.split(',');
95
+ for (const mutationId of mutationIds) {
96
+ const storedMutation = yield this.storage.getItem(this.prefix + mutationId);
97
+ if (!storedMutation) {
98
+ continue;
99
+ }
100
+ map.set(mutationId, JSON.parse(storedMutation));
101
+ }
102
+ return map;
103
+ }
104
+ catch (e) {
105
+ console.error(e);
106
+ return map;
107
+ }
108
+ });
109
+ }
110
+ /**
111
+ * Persist the queue so mutations can be retried at a later point in time.
112
+ */
113
+ saveQueue(mutationId, mutation) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ try {
116
+ if (mutationId && mutation) {
117
+ yield this.storage.setItem(this.prefix + mutationId, JSON.stringify(mutation));
118
+ }
119
+ yield this.storage.setItem(this.prefix + 'Mutations', [...this.queue.keys()].join());
120
+ }
121
+ catch (e) {
122
+ console.error(e);
123
+ }
124
+ });
125
+ }
126
+ /**
127
+ * Add a mutation attempt to the queue so that it can be retried at a later point in time.
128
+ */
129
+ add(item, mutationId) {
130
+ return __awaiter(this, void 0, void 0, function* () {
131
+ // We give the mutation attempt a random id so that it is easy to remove when needed (in sync loop)
132
+ if (!mutationId) {
133
+ mutationId = uuid4_1.default.asBase64();
134
+ }
135
+ this.queue.set(mutationId, item);
136
+ yield this.saveQueue(mutationId, item);
137
+ return mutationId;
138
+ });
139
+ }
140
+ /**
141
+ * Remove a mutation attempt from the queue.
142
+ */
143
+ remove(mutationId) {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ this.queue.delete(mutationId);
146
+ yield this.storage.removeItem(this.prefix + mutationId);
147
+ yield this.saveQueue();
148
+ });
149
+ }
150
+ /**
151
+ * Takes the mutations in the queue and try to send them to the server again.
152
+ */
153
+ sync() {
154
+ return __awaiter(this, void 0, void 0, function* () {
155
+ if (!this.client) {
156
+ throw new Error('Offline Link not setup with ApolloClient');
157
+ }
158
+ if (this.queue.size === 0) {
159
+ return;
160
+ }
161
+ const attempts = Array.from(this.queue);
162
+ if (this.sequential) {
163
+ for (const [mutationId, attempt] of attempts) {
164
+ let serverProcessed = false;
165
+ try {
166
+ yield this.client.mutate(Object.assign(Object.assign({}, attempt), { optimisticResponse: undefined }));
167
+ serverProcessed = true;
168
+ }
169
+ catch (e) {
170
+ const err = e;
171
+ const networkError = err.networkError;
172
+ if (!this.retryOnServerError && (networkError === null || networkError === void 0 ? void 0 : networkError.response)) {
173
+ // There are GraphQL errors, which means the server processed the request so we can remove the mutation from the queue
174
+ serverProcessed = true;
175
+ }
176
+ }
177
+ if (serverProcessed) {
178
+ try {
179
+ yield this.remove(mutationId);
180
+ }
181
+ catch (e) {
182
+ console.error(e);
183
+ }
184
+ }
185
+ }
186
+ }
187
+ else {
188
+ yield Promise.all(attempts.map(([mutationId, attempt]) => __awaiter(this, void 0, void 0, function* () {
189
+ var _a;
190
+ let serverProcessed = false;
191
+ try {
192
+ yield ((_a = this.client) === null || _a === void 0 ? void 0 : _a.mutate(Object.assign(Object.assign({}, attempt), { optimisticResponse: undefined })));
193
+ serverProcessed = true;
194
+ }
195
+ catch (e) {
196
+ const err = e;
197
+ const networkError = err.networkError;
198
+ if (!this.retryOnServerError && (networkError === null || networkError === void 0 ? void 0 : networkError.response)) {
199
+ serverProcessed = true;
200
+ }
201
+ }
202
+ if (serverProcessed) {
203
+ yield this.remove(mutationId);
204
+ }
205
+ })));
206
+ }
207
+ // Remaining mutations in the queue are persisted
208
+ yield this.saveQueue();
209
+ if (this.queue.size !== 0 || this.retryInterval !== 0) {
210
+ this.delayedSync();
211
+ }
212
+ });
213
+ }
214
+ /**
215
+ * Runs sync() after the retryInterval
216
+ */
217
+ delayedSync() {
218
+ if (this.retryInterval === 0) {
219
+ return;
220
+ }
221
+ if (this.timeoutId) {
222
+ clearTimeout(this.timeoutId);
223
+ }
224
+ this.timeoutId = setTimeout(() => this.sync, this.retryInterval);
225
+ }
226
+ /**
227
+ * Configure the link to use Apollo Client and immediately try to sync the queue (if there's anything there).
228
+ */
229
+ setup(client) {
230
+ return __awaiter(this, void 0, void 0, function* () {
231
+ this.client = client;
232
+ this.queue = yield this.getQueue();
233
+ yield this.sync();
234
+ });
235
+ }
236
+ }
237
+ exports.OfflineLink = OfflineLink;
File without changes
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { ApolloLink, Observable, } from '@apollo/client';
11
- import uuid from '../utils/uuid4';
11
+ import uuid from './uuid4';
12
12
  export class OfflineLink extends ApolloLink {
13
13
  /**
14
14
  * storage