@kubun/connector-google-calendar 0.17.1 → 0.17.3

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.
package/lib/target.js ADDED
@@ -0,0 +1,68 @@
1
+ import { deriveAccountKey } from './account-key.js';
2
+ import { splitEventSourceID } from './api.js';
3
+ import { GoogleCalendarError } from './errors.js';
4
+ import { googleCalendarDefinition } from './model.js';
5
+ function calendarError(code, message) {
6
+ return new GoogleCalendarError(`${code}: ${message}`, {
7
+ code,
8
+ kind: 'rejected'
9
+ });
10
+ }
11
+ // The account's primary calendar, as synced to this device: the calendar-list
12
+ // entry Google flags primary, under this account. Never a provider call —
13
+ // before the calendars are synced there is nothing to resolve it to.
14
+ async function syncedPrimaryCalendar(storage, providerAccount) {
15
+ const accountKey = deriveAccountKey(providerAccount);
16
+ const primary = (await storage.documents(googleCalendarDefinition.name)).filter((calendar)=>calendar.primary === true && calendar.accountKey === accountKey);
17
+ const [only] = primary;
18
+ if (primary.length !== 1 || typeof only?.sourceID !== 'string') {
19
+ throw calendarError('GOOGLE_CALENDARS_NOT_SYNCED', primary.length === 0 ? 'the primary calendar is not known on this device yet; sync calendars first' : 'more than one synced calendar is flagged primary for this account; sync calendars again');
20
+ }
21
+ return only.sourceID;
22
+ }
23
+ /**
24
+ * The Calendar target resolver (see `ActionTargetResolver`): a write's target
25
+ * is the calendar it lands in.
26
+ * - A create targets the `calendarID` of its input, required.
27
+ * - An update targets the calendar of its event (the `calendarID/eventID`
28
+ * sourceID); a `calendarID` in its input must name that same calendar.
29
+ * - The `'primary'` alias is resolved to the account's synced primary
30
+ * calendar, and the input carries the resolved id from then on.
31
+ */ export const resolveCalendarTarget = async ({ op, input, sourceID, providerAccount, storage })=>{
32
+ const named = input.calendarID;
33
+ const resolveNamed = async ()=>{
34
+ if (named == null) return undefined;
35
+ if (typeof named !== 'string' || named === '') {
36
+ throw calendarError('GOOGLE_CALENDAR_ID_REQUIRED', 'calendarID must be a calendar id');
37
+ }
38
+ return named === 'primary' ? await syncedPrimaryCalendar(storage, providerAccount) : named;
39
+ };
40
+ if (op === 'create') {
41
+ const calendarID = await resolveNamed();
42
+ if (calendarID == null) {
43
+ throw calendarError('GOOGLE_CALENDAR_ID_REQUIRED', 'a new event needs a calendarID');
44
+ }
45
+ return {
46
+ target: calendarID,
47
+ input: {
48
+ ...input,
49
+ calendarID
50
+ }
51
+ };
52
+ }
53
+ if (sourceID == null) {
54
+ throw calendarError('GOOGLE_CALENDAR_ID_REQUIRED', 'an event update needs its sourceID');
55
+ }
56
+ const eventCalendarID = splitEventSourceID(sourceID).calendarID;
57
+ const calendarID = await resolveNamed();
58
+ if (calendarID != null && calendarID !== eventCalendarID) {
59
+ throw calendarError('GOOGLE_CALENDAR_ID_MISMATCH', `calendarID does not name the calendar of the event "${sourceID}"`);
60
+ }
61
+ return {
62
+ target: eventCalendarID,
63
+ input: calendarID != null ? {
64
+ ...input,
65
+ calendarID
66
+ } : input
67
+ };
68
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/connector-google-calendar",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -15,12 +15,18 @@
15
15
  "LICENSE.md"
16
16
  ],
17
17
  "dependencies": {
18
- "@kubun/connector": "^0.17.1",
19
- "@kubun/credential-types": "^0.17.0",
18
+ "@kubun/connector": "^0.17.3",
19
+ "@kubun/credential-types": "^0.17.2",
20
+ "@kubun/hlc": "^0.17.0",
21
+ "@kubun/id": "^0.17.0",
20
22
  "@kubun/models": "^0.17.0",
21
- "@kubun/protocol": "^0.17.0",
22
- "@kubun/provider-google": "^0.17.0",
23
- "@kubun/scalars": "^0.17.0"
23
+ "@kubun/protocol": "^0.17.2",
24
+ "@kubun/provider-google": "^0.17.1",
25
+ "@kubun/scalars": "^0.17.0",
26
+ "@sozai/codec": "^0.4.0"
27
+ },
28
+ "devDependencies": {
29
+ "@sozai/schema": "^0.1.2"
24
30
  },
25
31
  "publishConfig": {
26
32
  "access": "public"