@mackenly/planningcenterkit 0.4.42 → 1.0.1
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/README.md +472 -31
- package/dist/cjs/client.d.ts +145 -0
- package/dist/cjs/client.d.ts.map +1 -0
- package/dist/cjs/client.js +176 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/core/auth.d.ts +105 -0
- package/dist/cjs/core/auth.d.ts.map +1 -0
- package/dist/cjs/core/auth.js +219 -0
- package/dist/cjs/core/auth.js.map +1 -0
- package/dist/cjs/core/errors.d.ts +145 -0
- package/dist/cjs/core/errors.d.ts.map +1 -0
- package/dist/cjs/core/errors.js +202 -0
- package/dist/cjs/core/errors.js.map +1 -0
- package/dist/cjs/core/graph.d.ts +110 -0
- package/dist/cjs/core/graph.d.ts.map +1 -0
- package/dist/cjs/core/graph.js +14 -0
- package/dist/cjs/core/graph.js.map +1 -0
- package/dist/cjs/core/http.d.ts +147 -0
- package/dist/cjs/core/http.d.ts.map +1 -0
- package/dist/cjs/core/http.js +478 -0
- package/dist/cjs/core/http.js.map +1 -0
- package/dist/cjs/core/jsonapi.d.ts +65 -0
- package/dist/cjs/core/jsonapi.d.ts.map +1 -0
- package/dist/{types.js → cjs/core/jsonapi.js} +1 -2
- package/dist/cjs/core/jsonapi.js.map +1 -0
- package/dist/cjs/core/oauth.d.ts +202 -0
- package/dist/cjs/core/oauth.d.ts.map +1 -0
- package/dist/cjs/core/oauth.js +290 -0
- package/dist/cjs/core/oauth.js.map +1 -0
- package/dist/cjs/core/product.d.ts +78 -0
- package/dist/cjs/core/product.d.ts.map +1 -0
- package/dist/cjs/core/product.js +57 -0
- package/dist/cjs/core/product.js.map +1 -0
- package/dist/cjs/core/query.d.ts +489 -0
- package/dist/cjs/core/query.d.ts.map +1 -0
- package/dist/cjs/core/query.js +435 -0
- package/dist/cjs/core/query.js.map +1 -0
- package/dist/cjs/core/transport.d.ts +57 -0
- package/dist/cjs/core/transport.d.ts.map +1 -0
- package/dist/cjs/core/transport.js +7 -0
- package/dist/cjs/core/transport.js.map +1 -0
- package/dist/cjs/entries/api.d.ts +26 -0
- package/dist/cjs/entries/api.d.ts.map +1 -0
- package/dist/cjs/entries/api.js +40 -0
- package/dist/cjs/entries/api.js.map +1 -0
- package/dist/cjs/entries/calendar.d.ts +26 -0
- package/dist/cjs/entries/calendar.d.ts.map +1 -0
- package/dist/cjs/entries/calendar.js +40 -0
- package/dist/cjs/entries/calendar.js.map +1 -0
- package/dist/cjs/entries/check-ins.d.ts +26 -0
- package/dist/cjs/entries/check-ins.d.ts.map +1 -0
- package/dist/cjs/entries/check-ins.js +40 -0
- package/dist/cjs/entries/check-ins.js.map +1 -0
- package/dist/cjs/entries/current.d.ts +26 -0
- package/dist/cjs/entries/current.d.ts.map +1 -0
- package/dist/cjs/entries/current.js +40 -0
- package/dist/cjs/entries/current.js.map +1 -0
- package/dist/cjs/entries/giving.d.ts +26 -0
- package/dist/cjs/entries/giving.d.ts.map +1 -0
- package/dist/cjs/entries/giving.js +40 -0
- package/dist/cjs/entries/giving.js.map +1 -0
- package/dist/cjs/entries/groups.d.ts +26 -0
- package/dist/cjs/entries/groups.d.ts.map +1 -0
- package/dist/cjs/entries/groups.js +40 -0
- package/dist/cjs/entries/groups.js.map +1 -0
- package/dist/cjs/entries/oauth.d.ts +8 -0
- package/dist/cjs/entries/oauth.d.ts.map +1 -0
- package/dist/cjs/entries/oauth.js +27 -0
- package/dist/cjs/entries/oauth.js.map +1 -0
- package/dist/cjs/entries/people.d.ts +26 -0
- package/dist/cjs/entries/people.d.ts.map +1 -0
- package/dist/cjs/entries/people.js +40 -0
- package/dist/cjs/entries/people.js.map +1 -0
- package/dist/cjs/entries/publishing.d.ts +26 -0
- package/dist/cjs/entries/publishing.d.ts.map +1 -0
- package/dist/cjs/entries/publishing.js +40 -0
- package/dist/cjs/entries/publishing.js.map +1 -0
- package/dist/cjs/entries/registrations.d.ts +26 -0
- package/dist/cjs/entries/registrations.d.ts.map +1 -0
- package/dist/cjs/entries/registrations.js +40 -0
- package/dist/cjs/entries/registrations.js.map +1 -0
- package/dist/cjs/entries/services.d.ts +26 -0
- package/dist/cjs/entries/services.d.ts.map +1 -0
- package/dist/cjs/entries/services.js +40 -0
- package/dist/cjs/entries/services.js.map +1 -0
- package/dist/cjs/entries/webhooks-api.d.ts +26 -0
- package/dist/cjs/entries/webhooks-api.d.ts.map +1 -0
- package/dist/cjs/entries/webhooks-api.js +40 -0
- package/dist/cjs/entries/webhooks-api.js.map +1 -0
- package/dist/cjs/entries/webhooks.d.ts +6 -0
- package/dist/cjs/entries/webhooks.d.ts.map +1 -0
- package/dist/cjs/entries/webhooks.js +22 -0
- package/dist/cjs/entries/webhooks.js.map +1 -0
- package/dist/cjs/generated/api.d.ts +426 -0
- package/dist/cjs/generated/api.d.ts.map +1 -0
- package/dist/cjs/generated/api.js +77 -0
- package/dist/cjs/generated/api.js.map +1 -0
- package/dist/cjs/generated/calendar.d.ts +2849 -0
- package/dist/cjs/generated/calendar.d.ts.map +1 -0
- package/dist/cjs/generated/calendar.js +299 -0
- package/dist/cjs/generated/calendar.js.map +1 -0
- package/dist/cjs/generated/check-ins.d.ts +2383 -0
- package/dist/cjs/generated/check-ins.d.ts.map +1 -0
- package/dist/cjs/generated/check-ins.js +298 -0
- package/dist/cjs/generated/check-ins.js.map +1 -0
- package/dist/cjs/generated/current.d.ts +135 -0
- package/dist/cjs/generated/current.d.ts.map +1 -0
- package/dist/cjs/generated/current.js +34 -0
- package/dist/cjs/generated/current.js.map +1 -0
- package/dist/cjs/generated/giving.d.ts +2309 -0
- package/dist/cjs/generated/giving.d.ts.map +1 -0
- package/dist/cjs/generated/giving.js +228 -0
- package/dist/cjs/generated/giving.js.map +1 -0
- package/dist/cjs/generated/groups.d.ts +1789 -0
- package/dist/cjs/generated/groups.d.ts.map +1 -0
- package/dist/cjs/generated/groups.js +200 -0
- package/dist/cjs/generated/groups.js.map +1 -0
- package/dist/cjs/generated/index.d.ts +5067 -0
- package/dist/cjs/generated/index.d.ts.map +1 -0
- package/dist/cjs/generated/index.js +73 -0
- package/dist/cjs/generated/index.js.map +1 -0
- package/dist/cjs/generated/people.d.ts +6001 -0
- package/dist/cjs/generated/people.d.ts.map +1 -0
- package/dist/cjs/generated/people.js +694 -0
- package/dist/cjs/generated/people.js.map +1 -0
- package/dist/cjs/generated/publishing.d.ts +1468 -0
- package/dist/cjs/generated/publishing.d.ts.map +1 -0
- package/dist/cjs/generated/publishing.js +180 -0
- package/dist/cjs/generated/publishing.js.map +1 -0
- package/dist/cjs/generated/registrations.d.ts +936 -0
- package/dist/cjs/generated/registrations.d.ts.map +1 -0
- package/dist/cjs/generated/registrations.js +122 -0
- package/dist/cjs/generated/registrations.js.map +1 -0
- package/dist/cjs/generated/services.d.ts +6301 -0
- package/dist/cjs/generated/services.d.ts.map +1 -0
- package/dist/cjs/generated/services.js +634 -0
- package/dist/cjs/generated/services.js.map +1 -0
- package/dist/cjs/generated/webhooks.d.ts +325 -0
- package/dist/cjs/generated/webhooks.d.ts.map +1 -0
- package/dist/cjs/generated/webhooks.js +58 -0
- package/dist/cjs/generated/webhooks.js.map +1 -0
- package/dist/cjs/index.d.ts +29 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +51 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/webhooks/errors.d.ts +51 -0
- package/dist/cjs/webhooks/errors.d.ts.map +1 -0
- package/dist/cjs/webhooks/errors.js +56 -0
- package/dist/cjs/webhooks/errors.js.map +1 -0
- package/dist/cjs/webhooks/events.d.ts +144 -0
- package/dist/cjs/webhooks/events.d.ts.map +1 -0
- package/dist/cjs/webhooks/events.js +160 -0
- package/dist/cjs/webhooks/events.js.map +1 -0
- package/dist/cjs/webhooks/handle.d.ts +103 -0
- package/dist/cjs/webhooks/handle.d.ts.map +1 -0
- package/dist/cjs/webhooks/handle.js +140 -0
- package/dist/cjs/webhooks/handle.js.map +1 -0
- package/dist/cjs/webhooks/index.d.ts +33 -0
- package/dist/cjs/webhooks/index.d.ts.map +1 -0
- package/dist/cjs/webhooks/index.js +46 -0
- package/dist/cjs/webhooks/index.js.map +1 -0
- package/dist/cjs/webhooks/signature.d.ts +84 -0
- package/dist/cjs/webhooks/signature.d.ts.map +1 -0
- package/dist/cjs/webhooks/signature.js +126 -0
- package/dist/cjs/webhooks/signature.js.map +1 -0
- package/dist/esm/client.d.ts +145 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/client.js +172 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/core/auth.d.ts +105 -0
- package/dist/esm/core/auth.d.ts.map +1 -0
- package/dist/esm/core/auth.js +215 -0
- package/dist/esm/core/auth.js.map +1 -0
- package/dist/esm/core/errors.d.ts +145 -0
- package/dist/esm/core/errors.d.ts.map +1 -0
- package/dist/esm/core/errors.js +185 -0
- package/dist/esm/core/errors.js.map +1 -0
- package/dist/esm/core/graph.d.ts +110 -0
- package/dist/esm/core/graph.d.ts.map +1 -0
- package/dist/esm/core/graph.js +13 -0
- package/dist/esm/core/graph.js.map +1 -0
- package/dist/esm/core/http.d.ts +147 -0
- package/dist/esm/core/http.d.ts.map +1 -0
- package/dist/esm/core/http.js +473 -0
- package/dist/esm/core/http.js.map +1 -0
- package/dist/esm/core/jsonapi.d.ts +65 -0
- package/dist/esm/core/jsonapi.d.ts.map +1 -0
- package/dist/esm/core/jsonapi.js +2 -0
- package/dist/esm/core/jsonapi.js.map +1 -0
- package/dist/esm/core/oauth.d.ts +202 -0
- package/dist/esm/core/oauth.d.ts.map +1 -0
- package/dist/esm/core/oauth.js +280 -0
- package/dist/esm/core/oauth.js.map +1 -0
- package/dist/esm/core/product.d.ts +78 -0
- package/dist/esm/core/product.d.ts.map +1 -0
- package/dist/esm/core/product.js +54 -0
- package/dist/esm/core/product.js.map +1 -0
- package/dist/esm/core/query.d.ts +489 -0
- package/dist/esm/core/query.d.ts.map +1 -0
- package/dist/esm/core/query.js +431 -0
- package/dist/esm/core/query.js.map +1 -0
- package/dist/esm/core/transport.d.ts +57 -0
- package/dist/esm/core/transport.d.ts.map +1 -0
- package/dist/esm/core/transport.js +6 -0
- package/dist/esm/core/transport.js.map +1 -0
- package/dist/esm/entries/api.d.ts +26 -0
- package/dist/esm/entries/api.d.ts.map +1 -0
- package/dist/esm/entries/api.js +23 -0
- package/dist/esm/entries/api.js.map +1 -0
- package/dist/esm/entries/calendar.d.ts +26 -0
- package/dist/esm/entries/calendar.d.ts.map +1 -0
- package/dist/esm/entries/calendar.js +23 -0
- package/dist/esm/entries/calendar.js.map +1 -0
- package/dist/esm/entries/check-ins.d.ts +26 -0
- package/dist/esm/entries/check-ins.d.ts.map +1 -0
- package/dist/esm/entries/check-ins.js +23 -0
- package/dist/esm/entries/check-ins.js.map +1 -0
- package/dist/esm/entries/current.d.ts +26 -0
- package/dist/esm/entries/current.d.ts.map +1 -0
- package/dist/esm/entries/current.js +23 -0
- package/dist/esm/entries/current.js.map +1 -0
- package/dist/esm/entries/giving.d.ts +26 -0
- package/dist/esm/entries/giving.d.ts.map +1 -0
- package/dist/esm/entries/giving.js +23 -0
- package/dist/esm/entries/giving.js.map +1 -0
- package/dist/esm/entries/groups.d.ts +26 -0
- package/dist/esm/entries/groups.d.ts.map +1 -0
- package/dist/esm/entries/groups.js +23 -0
- package/dist/esm/entries/groups.js.map +1 -0
- package/dist/esm/entries/oauth.d.ts +8 -0
- package/dist/esm/entries/oauth.d.ts.map +1 -0
- package/dist/esm/entries/oauth.js +7 -0
- package/dist/esm/entries/oauth.js.map +1 -0
- package/dist/esm/entries/people.d.ts +26 -0
- package/dist/esm/entries/people.d.ts.map +1 -0
- package/dist/esm/entries/people.js +23 -0
- package/dist/esm/entries/people.js.map +1 -0
- package/dist/esm/entries/publishing.d.ts +26 -0
- package/dist/esm/entries/publishing.d.ts.map +1 -0
- package/dist/esm/entries/publishing.js +23 -0
- package/dist/esm/entries/publishing.js.map +1 -0
- package/dist/esm/entries/registrations.d.ts +26 -0
- package/dist/esm/entries/registrations.d.ts.map +1 -0
- package/dist/esm/entries/registrations.js +23 -0
- package/dist/esm/entries/registrations.js.map +1 -0
- package/dist/esm/entries/services.d.ts +26 -0
- package/dist/esm/entries/services.d.ts.map +1 -0
- package/dist/esm/entries/services.js +23 -0
- package/dist/esm/entries/services.js.map +1 -0
- package/dist/esm/entries/webhooks-api.d.ts +26 -0
- package/dist/esm/entries/webhooks-api.d.ts.map +1 -0
- package/dist/esm/entries/webhooks-api.js +23 -0
- package/dist/esm/entries/webhooks-api.js.map +1 -0
- package/dist/esm/entries/webhooks.d.ts +6 -0
- package/dist/esm/entries/webhooks.d.ts.map +1 -0
- package/dist/esm/entries/webhooks.js +6 -0
- package/dist/esm/entries/webhooks.js.map +1 -0
- package/dist/esm/generated/api.d.ts +426 -0
- package/dist/esm/generated/api.d.ts.map +1 -0
- package/dist/esm/generated/api.js +74 -0
- package/dist/esm/generated/api.js.map +1 -0
- package/dist/esm/generated/calendar.d.ts +2849 -0
- package/dist/esm/generated/calendar.d.ts.map +1 -0
- package/dist/esm/generated/calendar.js +296 -0
- package/dist/esm/generated/calendar.js.map +1 -0
- package/dist/esm/generated/check-ins.d.ts +2383 -0
- package/dist/esm/generated/check-ins.d.ts.map +1 -0
- package/dist/esm/generated/check-ins.js +295 -0
- package/dist/esm/generated/check-ins.js.map +1 -0
- package/dist/esm/generated/current.d.ts +135 -0
- package/dist/esm/generated/current.d.ts.map +1 -0
- package/dist/esm/generated/current.js +31 -0
- package/dist/esm/generated/current.js.map +1 -0
- package/dist/esm/generated/giving.d.ts +2309 -0
- package/dist/esm/generated/giving.d.ts.map +1 -0
- package/dist/esm/generated/giving.js +225 -0
- package/dist/esm/generated/giving.js.map +1 -0
- package/dist/esm/generated/groups.d.ts +1789 -0
- package/dist/esm/generated/groups.d.ts.map +1 -0
- package/dist/esm/generated/groups.js +197 -0
- package/dist/esm/generated/groups.js.map +1 -0
- package/dist/esm/generated/index.d.ts +5067 -0
- package/dist/esm/generated/index.d.ts.map +1 -0
- package/dist/esm/generated/index.js +37 -0
- package/dist/esm/generated/index.js.map +1 -0
- package/dist/esm/generated/people.d.ts +6001 -0
- package/dist/esm/generated/people.d.ts.map +1 -0
- package/dist/esm/generated/people.js +691 -0
- package/dist/esm/generated/people.js.map +1 -0
- package/dist/esm/generated/publishing.d.ts +1468 -0
- package/dist/esm/generated/publishing.d.ts.map +1 -0
- package/dist/esm/generated/publishing.js +177 -0
- package/dist/esm/generated/publishing.js.map +1 -0
- package/dist/esm/generated/registrations.d.ts +936 -0
- package/dist/esm/generated/registrations.d.ts.map +1 -0
- package/dist/esm/generated/registrations.js +119 -0
- package/dist/esm/generated/registrations.js.map +1 -0
- package/dist/esm/generated/services.d.ts +6301 -0
- package/dist/esm/generated/services.d.ts.map +1 -0
- package/dist/esm/generated/services.js +631 -0
- package/dist/esm/generated/services.js.map +1 -0
- package/dist/esm/generated/webhooks.d.ts +325 -0
- package/dist/esm/generated/webhooks.d.ts.map +1 -0
- package/dist/esm/generated/webhooks.js +55 -0
- package/dist/esm/generated/webhooks.js.map +1 -0
- package/dist/esm/index.d.ts +29 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +26 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/webhooks/errors.d.ts +51 -0
- package/dist/esm/webhooks/errors.d.ts.map +1 -0
- package/dist/esm/webhooks/errors.js +51 -0
- package/dist/esm/webhooks/errors.js.map +1 -0
- package/dist/esm/webhooks/events.d.ts +144 -0
- package/dist/esm/webhooks/events.d.ts.map +1 -0
- package/dist/esm/webhooks/events.js +155 -0
- package/dist/esm/webhooks/events.js.map +1 -0
- package/dist/esm/webhooks/handle.d.ts +103 -0
- package/dist/esm/webhooks/handle.d.ts.map +1 -0
- package/dist/esm/webhooks/handle.js +137 -0
- package/dist/esm/webhooks/handle.js.map +1 -0
- package/dist/esm/webhooks/index.d.ts +33 -0
- package/dist/esm/webhooks/index.d.ts.map +1 -0
- package/dist/esm/webhooks/index.js +33 -0
- package/dist/esm/webhooks/index.js.map +1 -0
- package/dist/esm/webhooks/signature.d.ts +84 -0
- package/dist/esm/webhooks/signature.d.ts.map +1 -0
- package/dist/esm/webhooks/signature.js +120 -0
- package/dist/esm/webhooks/signature.js.map +1 -0
- package/llms.txt +119 -0
- package/package.json +79 -14
- package/src/client.ts +169 -282
- package/src/core/auth.ts +317 -0
- package/src/core/errors.ts +219 -0
- package/src/core/graph.ts +137 -0
- package/src/core/http.ts +594 -0
- package/src/core/jsonapi.ts +64 -0
- package/src/core/oauth.ts +403 -0
- package/src/core/product.ts +137 -0
- package/src/core/query.ts +1011 -0
- package/src/core/transport.ts +62 -0
- package/src/entries/api.ts +28 -0
- package/src/entries/calendar.ts +28 -0
- package/src/entries/check-ins.ts +28 -0
- package/src/entries/current.ts +28 -0
- package/src/entries/giving.ts +28 -0
- package/src/entries/groups.ts +28 -0
- package/src/entries/oauth.ts +7 -0
- package/src/entries/people.ts +28 -0
- package/src/entries/publishing.ts +28 -0
- package/src/entries/registrations.ts +28 -0
- package/src/entries/services.ts +28 -0
- package/src/entries/webhooks-api.ts +28 -0
- package/src/entries/webhooks.ts +5 -0
- package/src/generated/api.ts +401 -0
- package/src/generated/calendar.ts +2380 -0
- package/src/generated/check-ins.ts +1783 -0
- package/src/generated/current.ts +136 -0
- package/src/generated/giving.ts +1956 -0
- package/src/generated/groups.ts +1474 -0
- package/src/generated/index.ts +59 -0
- package/src/generated/people.ts +5103 -0
- package/src/generated/publishing.ts +1321 -0
- package/src/generated/registrations.ts +799 -0
- package/src/generated/services.ts +5427 -0
- package/src/generated/webhooks.ts +314 -0
- package/src/index.ts +74 -4
- package/src/webhooks/errors.ts +52 -0
- package/src/webhooks/events.ts +272 -0
- package/src/webhooks/handle.ts +163 -0
- package/src/webhooks/index.ts +54 -0
- package/src/webhooks/signature.ts +140 -0
- package/.gitattributes +0 -2
- package/.github/FUNDING.yml +0 -3
- package/.github/dependabot.yml +0 -21
- package/.github/workflows/ci-publish.yml +0 -84
- package/SECURITY.md +0 -13
- package/TODO.md +0 -6
- package/dist/client.d.ts +0 -216
- package/dist/client.js +0 -265
- package/dist/client.js.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -6
- package/dist/index.js.map +0 -1
- package/dist/modules/calendar.d.ts +0 -5
- package/dist/modules/calendar.js +0 -11
- package/dist/modules/calendar.js.map +0 -1
- package/dist/modules/checkins.d.ts +0 -0
- package/dist/modules/checkins.js +0 -2
- package/dist/modules/checkins.js.map +0 -1
- package/dist/modules/giving.d.ts +0 -0
- package/dist/modules/giving.js +0 -2
- package/dist/modules/giving.js.map +0 -1
- package/dist/modules/groups.d.ts +0 -4
- package/dist/modules/groups.js +0 -22
- package/dist/modules/groups.js.map +0 -1
- package/dist/modules/people.d.ts +0 -66
- package/dist/modules/people.js +0 -40
- package/dist/modules/people.js.map +0 -1
- package/dist/modules/publishing.d.ts +0 -0
- package/dist/modules/publishing.js +0 -2
- package/dist/modules/publishing.js.map +0 -1
- package/dist/modules/services.d.ts +0 -0
- package/dist/modules/services.js +0 -2
- package/dist/modules/services.js.map +0 -1
- package/dist/modules/webhooks.d.ts +0 -0
- package/dist/modules/webhooks.js +0 -2
- package/dist/modules/webhooks.js.map +0 -1
- package/dist/types.d.ts +0 -6
- package/dist/types.js.map +0 -1
- package/src/index.test.ts +0 -7
- package/src/modules/calendar.ts +0 -11
- package/src/modules/checkins.ts +0 -0
- package/src/modules/giving.ts +0 -0
- package/src/modules/groups.ts +0 -7
- package/src/modules/people.ts +0 -92
- package/src/modules/publishing.ts +0 -0
- package/src/modules/services.ts +0 -0
- package/src/modules/webhooks.ts +0 -0
- package/src/types.ts +0 -8
- package/tsconfig.json +0 -111
|
@@ -0,0 +1,1011 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fluent, immutable query builder used for every resource in every product.
|
|
3
|
+
*
|
|
4
|
+
* A product client (see `createProductClient` in `product.ts`) exposes one builder per edge:
|
|
5
|
+
* - a {@link Collection} for edges that return many resources (`pco.people.people`),
|
|
6
|
+
* - an {@link ItemRef} for one resource by id (`pco.people.people.id('1')`),
|
|
7
|
+
* - a {@link SingleRef} for edges that return one resource (`pco.groups.groups.id('5').enrollment`).
|
|
8
|
+
*
|
|
9
|
+
* Every builder method returns a new builder, so a base query can be shared and branched safely.
|
|
10
|
+
* Names (edges, attributes, includes, where/order fields) are snake_case exactly as in
|
|
11
|
+
* Planning Center's docs.
|
|
12
|
+
*
|
|
13
|
+
* @see https://developer.planning.center/docs/#/overview/
|
|
14
|
+
*/
|
|
15
|
+
import { PcoError } from './errors.js';
|
|
16
|
+
import type { EdgeMeta, EdgeSchema, ProductGraph, ProductSchema, VertexMeta, VertexSchema, WhereOperator } from './graph.js';
|
|
17
|
+
import type { AnyResource, JsonApiDocument, PcoListMeta, PcoResource, Relationship, ResourceIdentifier } from './jsonapi.js';
|
|
18
|
+
import type { PcoTransport, QueryValue } from './transport.js';
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Public types
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
/** A resource id: a non-empty string or a finite number. */
|
|
25
|
+
export type ResourceId = string | number;
|
|
26
|
+
|
|
27
|
+
/** Options accepted by every request. */
|
|
28
|
+
export interface RequestOptions {
|
|
29
|
+
/** Cancels the request. */
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Options for fetching a single resource.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* await pco.people.people.id('123').get({ include: ['emails'] });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export interface GetOptions<V extends VertexSchema = VertexSchema> extends RequestOptions {
|
|
42
|
+
/** Related resources to side-load into `included`. */
|
|
43
|
+
include?: readonly V['includes'][];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Options for creating or updating a resource.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* await pco.people.people.id('1').emails.create(
|
|
52
|
+
* { address: 'ada@example.com', location: 'Home' },
|
|
53
|
+
* { include: ['person'] },
|
|
54
|
+
* );
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export interface WriteOptions<V extends VertexSchema = VertexSchema> extends GetOptions<V> {
|
|
58
|
+
/** JSON:API relationships to send with the resource, keyed by relationship name. */
|
|
59
|
+
relationships?: Record<string, { data: ResourceIdentifier | ResourceIdentifier[] | null }>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Options for {@link Collection.all} and {@link Collection.toArray}.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const first500 = await pco.people.people.toArray({ max: 500 });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export interface IterateOptions extends RequestOptions {
|
|
71
|
+
/** Stop after this many resources. */
|
|
72
|
+
max?: number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Options for {@link Collection.pages}.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* for await (const page of pco.people.people.pages({ maxPages: 3 })) console.log(page.data.length);
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export interface PagesOptions extends RequestOptions {
|
|
84
|
+
/** Stop after this many pages. */
|
|
85
|
+
maxPages?: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** An `order` value: a field name, or the field name prefixed with `-` for descending. */
|
|
89
|
+
export type OrderValue<F extends string> = F | `-${F}`;
|
|
90
|
+
|
|
91
|
+
/** Anything with relationships that {@link ListResult.related} can resolve. */
|
|
92
|
+
export interface RelatedSource {
|
|
93
|
+
type: string;
|
|
94
|
+
id: string;
|
|
95
|
+
relationships?: object;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The resource type returned by `related(resource, name)`: typed from the vertex's `related`
|
|
100
|
+
* map when the name is known, otherwise {@link AnyResource}.
|
|
101
|
+
*/
|
|
102
|
+
export type RelatedResource<S extends ProductSchema, K extends keyof S, N extends string> =
|
|
103
|
+
N extends keyof S[K]['related']
|
|
104
|
+
? S[K]['related'][N] extends keyof S
|
|
105
|
+
? PcoResource<S[S[K]['related'][N]]>
|
|
106
|
+
: AnyResource
|
|
107
|
+
: AnyResource;
|
|
108
|
+
|
|
109
|
+
/** Relationship names offered by `related()` autocomplete; any string is accepted. */
|
|
110
|
+
export type RelatedName<V extends VertexSchema> = (keyof V['related'] & string) | V['relationships'] | (string & {});
|
|
111
|
+
|
|
112
|
+
/** Shared shape of {@link ListResult} and {@link ItemResult}. */
|
|
113
|
+
export interface ResultBase<S extends ProductSchema, K extends keyof S> {
|
|
114
|
+
/** Side-loaded resources requested with `include`. */
|
|
115
|
+
included: AnyResource[];
|
|
116
|
+
/** Response `meta`. */
|
|
117
|
+
meta: PcoListMeta;
|
|
118
|
+
/** Response `links`. */
|
|
119
|
+
links: Record<string, string | null>;
|
|
120
|
+
/**
|
|
121
|
+
* Looks up the resources a relationship points to in this response's `included` (and
|
|
122
|
+
* primary `data`). Always returns an array: empty when the relationship is missing or
|
|
123
|
+
* wasn't included, one element for a to-one relationship.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const { data, related } = await pco.people.people.include('emails').list();
|
|
128
|
+
* for (const person of data) {
|
|
129
|
+
* const emails = related(person, 'emails'); // PcoResource<email schema>[]
|
|
130
|
+
* console.log(person.attributes.name, emails.map((e) => e.attributes.address));
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
related<N extends RelatedName<S[K]>>(resource: RelatedSource, name: N): RelatedResource<S, K, N>[];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* One page of a collection.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const page = await pco.people.people.perPage(25).list();
|
|
143
|
+
* page.data; // PcoResource<person schema>[]
|
|
144
|
+
* page.totalCount; // total matching people
|
|
145
|
+
* page.nextOffset; // offset of the next page, if any
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
export interface ListResult<S extends ProductSchema, K extends keyof S> extends ResultBase<S, K> {
|
|
149
|
+
/** The resources on this page. */
|
|
150
|
+
data: PcoResource<S[K]>[];
|
|
151
|
+
/** `meta.total_count`, when the API returned it. */
|
|
152
|
+
totalCount?: number;
|
|
153
|
+
/** `meta.next.offset`, when there is another page. */
|
|
154
|
+
nextOffset?: number;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A single resource response.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* const { data } = await pco.people.people.id('123').get();
|
|
163
|
+
* data.attributes.first_name;
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export interface ItemResult<S extends ProductSchema, K extends keyof S, D = PcoResource<S[K]>> extends ResultBase<S, K> {
|
|
167
|
+
/** The resource. */
|
|
168
|
+
data: D;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* A collection endpoint, e.g. `pco.people.people` or `pco.people.people.id('1').emails`.
|
|
173
|
+
*
|
|
174
|
+
* Immutable: every builder method returns a new collection, so you can branch from a shared base.
|
|
175
|
+
* Type parameters: `S` is the product schema, `K` the vertex id of the resources, `F` the union of
|
|
176
|
+
* accepted `filter` values.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const recent = pco.people.people.where('created_at', 'gte', '2026-01-01').order('-created_at');
|
|
181
|
+
* const page = await recent.perPage(50).list();
|
|
182
|
+
* for await (const person of recent.all()) console.log(person.attributes.name);
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export interface Collection<S extends ProductSchema, K extends keyof S & string, F extends string = never> {
|
|
186
|
+
/** The collection's URL path, e.g. `/people/v2/people/1/emails`. For debugging. */
|
|
187
|
+
readonly path: string;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Adds an equality condition: `where[field]=value`.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* pco.people.people.where('first_name', 'Tim%').where('status', 'active');
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
where<P extends keyof S[K]['where'] & string>(field: P, value: S[K]['where'][P]): this;
|
|
198
|
+
/**
|
|
199
|
+
* Adds a comparison: `where[field][op]=value`. Only fields that support comparison accept an
|
|
200
|
+
* operator. Combine two calls for a range.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* pco.people.people.where('created_at', 'gte', '2026-01-01').where('created_at', 'lt', '2026-02-01');
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
where<P extends keyof S[K]['whereOps'] & keyof S[K]['where'] & string>(
|
|
208
|
+
field: P,
|
|
209
|
+
op: S[K]['whereOps'][P],
|
|
210
|
+
value: S[K]['where'][P],
|
|
211
|
+
): this;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Applies named filters: `filter=a,b`. Repeated calls add to the list.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* pco.services.service_types.id('1').plans.filter('future');
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
filter(...names: F[]): this;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Side-loads related resources: `include=a,b`. Repeated calls add to the list. Read them with
|
|
225
|
+
* `related()` on the result.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* pco.people.people.include('emails', 'phone_numbers');
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
include(...names: S[K]['includes'][]): this;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Sorts results: `order=a,-b`. Prefix a field with `-` for descending. Replaces any previous order.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* pco.people.people.order('-created_at');
|
|
240
|
+
* pco.people.people.order('last_name', 'first_name');
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
order(...fields: OrderValue<S[K]['order']>[]): this;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Page size, 1 to 100 (`per_page`). `all()` defaults to 100 when unset.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* pco.people.people.perPage(50);
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
perPage(n: number): this;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Skips this many resources (`offset`).
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* pco.people.people.perPage(25).offset(50).list(); // third page
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
offset(n: number): this;
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Sets a raw query parameter, for anything not covered by the typed methods.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* pco.people.people.param('where[search_name_or_email]', 'ada');
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
param(key: string, value: QueryValue): this;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Fetches one page.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* const { data, included, meta } = await pco.people.people.list();
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
list(options?: RequestOptions): Promise<ListResult<S, K>>;
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Iterates every matching resource, following pagination (`per_page` defaults to 100).
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* for await (const person of pco.people.people.where('status', 'active').all()) {
|
|
291
|
+
* console.log(person.attributes.name);
|
|
292
|
+
* }
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
all(options?: IterateOptions): AsyncIterable<PcoResource<S[K]>>;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Iterates page by page, following pagination (`per_page` defaults to 100). Useful when you
|
|
299
|
+
* need each page's `included`.
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* ```ts
|
|
303
|
+
* for await (const page of pco.people.people.include('emails').pages()) {
|
|
304
|
+
* for (const person of page.data) page.related(person, 'emails');
|
|
305
|
+
* }
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
pages(options?: PagesOptions): AsyncIterable<ListResult<S, K>>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Collects every matching resource (or the first `max`) into an array.
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* const people = await pco.people.people.where('status', 'active').toArray({ max: 1000 });
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
toArray(options?: IterateOptions): Promise<PcoResource<S[K]>[]>;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Fetches the first matching resource (`per_page=1`), or `undefined` if there is none.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* const newest = await pco.people.people.order('-created_at').first();
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
first(options?: RequestOptions): Promise<PcoResource<S[K]> | undefined>;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Returns `meta.total_count` for the query (fetches `per_page=1`).
|
|
332
|
+
*
|
|
333
|
+
* @example
|
|
334
|
+
* ```ts
|
|
335
|
+
* const active = await pco.people.people.where('status', 'active').count();
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
count(options?: RequestOptions): Promise<number>;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Fetches one resource by id. Uses this builder's `include` unless `options.include` is given.
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* const { data } = await pco.people.people.get('123', { include: ['emails'] });
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
348
|
+
get(id: ResourceId, options?: GetOptions<S[K]>): Promise<ItemResult<S, K>>;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* A reference to one resource by id, for updating, deleting, running actions, or following edges.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* const emails = await pco.people.people.id('123').emails.list();
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
id(id: ResourceId): ItemRef<S, K>;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* A {@link Collection} whose edge accepts `POST` to create resources.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* const { data } = await pco.people.people.create({ first_name: 'Ada', last_name: 'Lovelace' });
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
export interface CreatableCollection<S extends ProductSchema, K extends keyof S & string, F extends string = never>
|
|
370
|
+
extends Collection<S, K, F> {
|
|
371
|
+
/**
|
|
372
|
+
* Creates a resource: `POST` to this collection with `{ data: { type, attributes, relationships } }`.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* await pco.people.people.id('123').emails.create({ address: 'ada@example.com', location: 'Home' });
|
|
377
|
+
* ```
|
|
378
|
+
*/
|
|
379
|
+
create(attributes: S[K]['create'], options?: WriteOptions<S[K]>): Promise<ItemResult<S, K>>;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Methods available on every {@link ItemRef}.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```ts
|
|
387
|
+
* const ref = pco.people.people.id('123');
|
|
388
|
+
* await ref.get();
|
|
389
|
+
* await pco.people.people.id('1').workflow_cards.id('2').action('promote');
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
392
|
+
export interface ItemRefBase<S extends ProductSchema, K extends keyof S & string> {
|
|
393
|
+
/** The resource's URL path, e.g. `/people/v2/people/123`. For debugging. */
|
|
394
|
+
readonly path: string;
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Fetches the resource.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```ts
|
|
401
|
+
* const { data, related } = await pco.people.people.id('123').get({ include: ['emails'] });
|
|
402
|
+
* ```
|
|
403
|
+
*/
|
|
404
|
+
get(options?: GetOptions<S[K]>): Promise<ItemResult<S, K>>;
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Runs an action: `POST ${path}/${name}` with `body` sent as-is. Returns the parsed response
|
|
408
|
+
* body (`undefined` for 204).
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```ts
|
|
412
|
+
* await pco.services.service_types.id('1').plans.id('2').action('item_reorder', {
|
|
413
|
+
* data: { attributes: { sequence: ['5', '3', '4'] } },
|
|
414
|
+
* });
|
|
415
|
+
* ```
|
|
416
|
+
*/
|
|
417
|
+
action<T = JsonApiDocument | undefined>(name: S[K]['actions'], body?: unknown, options?: RequestOptions): Promise<T>;
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Follows an edge by name. Same as the edge property, and the only way to reach an edge whose
|
|
421
|
+
* name collides with a method (`get`, `update`, `delete`, `action`, `edge`, `path`, `then`).
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* pco.people.people.id('123').edge('emails').list();
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
edge<N extends keyof S[K]['edges'] & string>(name: N): EdgeBuilder<S, S[K]['edges'][N]>;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Present on an {@link ItemRef} when the resource type accepts updates.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```ts
|
|
436
|
+
* await pco.people.people.id('123').update({ nickname: 'Ada' });
|
|
437
|
+
* ```
|
|
438
|
+
*/
|
|
439
|
+
export interface Updatable<S extends ProductSchema, K extends keyof S & string> {
|
|
440
|
+
/**
|
|
441
|
+
* Updates the resource: `PATCH` with `{ data: { type, id, attributes, relationships } }`.
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* ```ts
|
|
445
|
+
* const { data } = await pco.people.people.id('123').update({ nickname: 'Ada' });
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
update(attributes: S[K]['update'], options?: WriteOptions<S[K]>): Promise<ItemResult<S, K>>;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Present on an {@link ItemRef} when the resource type can be deleted.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```ts
|
|
456
|
+
* await pco.people.people.id('123').delete();
|
|
457
|
+
* ```
|
|
458
|
+
*/
|
|
459
|
+
export interface Deletable {
|
|
460
|
+
/**
|
|
461
|
+
* Deletes the resource (`DELETE`). The runtime also refuses (with a {@link PcoError}, before
|
|
462
|
+
* sending anything) if the API doesn't support deleting this resource type.
|
|
463
|
+
*/
|
|
464
|
+
delete(options?: RequestOptions): Promise<void>;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Names that are methods on {@link ItemRef}; edges with these names are reachable via `edge()` only. */
|
|
468
|
+
export type ItemReservedName = 'get' | 'update' | 'delete' | 'action' | 'edge' | 'path' | 'then';
|
|
469
|
+
|
|
470
|
+
/** One property per edge of vertex `K`, excluding {@link ItemReservedName}s. */
|
|
471
|
+
export type ItemEdges<S extends ProductSchema, K extends keyof S & string> = {
|
|
472
|
+
readonly [N in Exclude<keyof S[K]['edges'] & string, ItemReservedName>]: EdgeBuilder<S, S[K]['edges'][N]>;
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* One resource by id: `get`, `update` and `delete` (when supported), `action`, and a property for
|
|
477
|
+
* every edge you can follow from it.
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* ```ts
|
|
481
|
+
* const person = pco.people.people.id('123');
|
|
482
|
+
* await person.update({ nickname: 'Ada' });
|
|
483
|
+
* const emails = await person.emails.list();
|
|
484
|
+
* const enrollment = await pco.groups.groups.id('5').enrollment.get();
|
|
485
|
+
* const plan = await pco.services.service_types.id('1').plans.id('2').get({ include: ['plan_times'] });
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
export type ItemRef<S extends ProductSchema, K extends keyof S & string> = ItemRefBase<S, K> &
|
|
489
|
+
ItemEdges<S, K> &
|
|
490
|
+
([keyof S[K]['update']] extends [never] ? unknown : Updatable<S, K>) &
|
|
491
|
+
(S[K]['delete'] extends true ? Deletable : unknown);
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* An edge that returns a single resource, e.g. `pco.groups.groups.id('5').enrollment`.
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* ```ts
|
|
498
|
+
* const { data: enrollment } = await pco.groups.groups.id('5').enrollment.get();
|
|
499
|
+
* enrollment?.attributes.status;
|
|
500
|
+
* ```
|
|
501
|
+
*/
|
|
502
|
+
export interface SingleRef<S extends ProductSchema, K extends keyof S & string> {
|
|
503
|
+
/** The edge's URL path, e.g. `/groups/v2/groups/5/enrollment`. For debugging. */
|
|
504
|
+
readonly path: string;
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Fetches the resource. `data` is `null` when the to-one relationship is empty.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* ```ts
|
|
511
|
+
* const { data } = await pco.groups.groups.id('5').enrollment.get();
|
|
512
|
+
* ```
|
|
513
|
+
*/
|
|
514
|
+
get(options?: GetOptions<S[K]>): Promise<ItemResult<S, K, PcoResource<S[K]> | null>>;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* A {@link SingleRef} whose resource can be created by `POST`ing to the edge, e.g. starring a list.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* await pco.people.lists.id('1').star.create({});
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
export interface CreatableSingleRef<S extends ProductSchema, K extends keyof S & string> extends SingleRef<S, K> {
|
|
526
|
+
/** Creates the resource: `POST` to this edge with `{ data: { type, attributes, relationships } }`. */
|
|
527
|
+
create(attributes: S[K]['create'], options?: WriteOptions<S[K]>): Promise<ItemResult<S, K>>;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* The builder for an edge: {@link CreatableCollection} or {@link Collection} for `many` edges,
|
|
532
|
+
* {@link SingleRef} otherwise.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* type Emails = EdgeBuilder<PeopleSchema, PeopleSchema['person']['edges']['emails']>;
|
|
537
|
+
* ```
|
|
538
|
+
*/
|
|
539
|
+
export type EdgeBuilder<S extends ProductSchema, E extends EdgeSchema> = E['vertex'] extends keyof S & string
|
|
540
|
+
? E['many'] extends true
|
|
541
|
+
? E['create'] extends true
|
|
542
|
+
? CreatableCollection<S, E['vertex'], E['filters']>
|
|
543
|
+
: Collection<S, E['vertex'], E['filters']>
|
|
544
|
+
: E['create'] extends true
|
|
545
|
+
? CreatableSingleRef<S, E['vertex']>
|
|
546
|
+
: SingleRef<S, E['vertex']>
|
|
547
|
+
: never;
|
|
548
|
+
|
|
549
|
+
// ---------------------------------------------------------------------------
|
|
550
|
+
// Runtime
|
|
551
|
+
// ---------------------------------------------------------------------------
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Shared state for every builder of one product client.
|
|
555
|
+
* @internal
|
|
556
|
+
*/
|
|
557
|
+
export interface QueryContext {
|
|
558
|
+
readonly graph: ProductGraph;
|
|
559
|
+
readonly transport: PcoTransport;
|
|
560
|
+
/** Sent as `version` on every request. */
|
|
561
|
+
readonly version: string;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
const WHERE_OPERATORS: ReadonlySet<string> = new Set<WhereOperator>(['gt', 'gte', 'lt', 'lte']);
|
|
565
|
+
|
|
566
|
+
interface CollectionState {
|
|
567
|
+
readonly where: Readonly<Record<string, QueryValue>>;
|
|
568
|
+
readonly params: Readonly<Record<string, QueryValue>>;
|
|
569
|
+
readonly include: readonly string[];
|
|
570
|
+
readonly filter: readonly string[];
|
|
571
|
+
readonly order: readonly string[];
|
|
572
|
+
readonly perPage?: number;
|
|
573
|
+
readonly offset?: number;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const EMPTY_STATE: CollectionState = { where: {}, params: {}, include: [], filter: [], order: [] };
|
|
577
|
+
|
|
578
|
+
function vertexMeta(ctx: QueryContext, vertex: string): VertexMeta {
|
|
579
|
+
const meta = ctx.graph.vertices[vertex];
|
|
580
|
+
if (!meta) throw new PcoError(`Unknown ${ctx.graph.product} resource type "${vertex}".`);
|
|
581
|
+
return meta;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Validates an id. Rejects dot segments (`.`, `..`, and encoded forms): URL parsing resolves them,
|
|
586
|
+
* so `emails.id('..').delete()` would otherwise delete the parent resource.
|
|
587
|
+
*/
|
|
588
|
+
function assertId(id: unknown): string {
|
|
589
|
+
if (typeof id === 'number' && Number.isFinite(id)) return String(id);
|
|
590
|
+
if (typeof id === 'string' && /^(?:\.|%2e){1,2}$/i.test(id.trim())) {
|
|
591
|
+
throw new PcoError(`Invalid resource id ${JSON.stringify(id)}: "." and ".." would change which URL is requested.`);
|
|
592
|
+
}
|
|
593
|
+
if (typeof id === 'string' && id.trim() !== '') return id;
|
|
594
|
+
throw new PcoError(`Invalid resource id ${JSON.stringify(id) ?? String(id)}: expected a non-empty string or a number.`);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** URL of one item: under the vertex's canonical collection when it has one, else under `collectionPath`. */
|
|
598
|
+
function itemPath(ctx: QueryContext, vertex: string, collectionPath: string, id: string): string {
|
|
599
|
+
const canonical = ctx.graph.vertices[vertex]?.canonicalPath;
|
|
600
|
+
return `${canonical ?? collectionPath}/${encodeURIComponent(id)}`;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function assertNames(kind: string, names: readonly unknown[]): string[] {
|
|
604
|
+
for (const name of names) {
|
|
605
|
+
if (typeof name !== 'string' || name === '') throw new PcoError(`Invalid ${kind} name ${JSON.stringify(name)}.`);
|
|
606
|
+
}
|
|
607
|
+
return names as string[];
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function merge(existing: readonly string[], added: readonly string[]): string[] {
|
|
611
|
+
return [...new Set([...existing, ...added])];
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function asDocument(body: unknown): JsonApiDocument {
|
|
615
|
+
if (body && typeof body === 'object') return body as JsonApiDocument;
|
|
616
|
+
return { data: null };
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function resourceKey(type: string, id: string): string {
|
|
620
|
+
return `${type}\u0000${id}`;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function makeRelated(doc: JsonApiDocument): (resource: RelatedSource, name: string) => AnyResource[] {
|
|
624
|
+
let index: Map<string, AnyResource> | undefined;
|
|
625
|
+
return (resource, name) => {
|
|
626
|
+
const rels = resource?.relationships as Record<string, Relationship | undefined> | undefined;
|
|
627
|
+
const linkage = rels?.[name]?.data;
|
|
628
|
+
if (!linkage) return [];
|
|
629
|
+
if (!index) {
|
|
630
|
+
index = new Map();
|
|
631
|
+
const primary = Array.isArray(doc.data) ? doc.data : doc.data ? [doc.data] : [];
|
|
632
|
+
for (const r of [...primary, ...(doc.included ?? [])]) index.set(resourceKey(r.type, r.id), r);
|
|
633
|
+
}
|
|
634
|
+
const ids = Array.isArray(linkage) ? linkage : [linkage];
|
|
635
|
+
const found: AnyResource[] = [];
|
|
636
|
+
for (const ref of ids) {
|
|
637
|
+
const hit = index.get(resourceKey(ref.type, ref.id));
|
|
638
|
+
if (hit) found.push(hit);
|
|
639
|
+
}
|
|
640
|
+
return found;
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function toListResult(body: unknown): ListResult<ProductSchema, string> {
|
|
645
|
+
const doc = asDocument(body);
|
|
646
|
+
const meta = doc.meta ?? {};
|
|
647
|
+
return {
|
|
648
|
+
// Some edges are documented as collections but return a single resource; accept both.
|
|
649
|
+
data: (Array.isArray(doc.data) ? doc.data : doc.data ? [doc.data] : []) as PcoResource[],
|
|
650
|
+
included: doc.included ?? [],
|
|
651
|
+
meta,
|
|
652
|
+
links: doc.links ?? {},
|
|
653
|
+
totalCount: typeof meta.total_count === 'number' ? meta.total_count : undefined,
|
|
654
|
+
nextOffset: typeof meta.next?.offset === 'number' ? meta.next.offset : undefined,
|
|
655
|
+
related: makeRelated(doc) as ListResult<ProductSchema, string>['related'],
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function toItemResult(body: unknown): ItemResult<ProductSchema, string, PcoResource | null> {
|
|
660
|
+
const doc = asDocument(body);
|
|
661
|
+
return {
|
|
662
|
+
data: (Array.isArray(doc.data) ? (doc.data[0] ?? null) : (doc.data ?? null)) as PcoResource | null,
|
|
663
|
+
included: doc.included ?? [],
|
|
664
|
+
meta: doc.meta ?? {},
|
|
665
|
+
links: doc.links ?? {},
|
|
666
|
+
related: makeRelated(doc) as ItemResult<ProductSchema, string>['related'],
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function includeQuery(include: readonly unknown[] | undefined): Record<string, QueryValue> {
|
|
671
|
+
return include && include.length ? { include: assertNames('include', include) } : {};
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
async function getItem(ctx: QueryContext, path: string, include: readonly unknown[] | undefined, signal?: AbortSignal) {
|
|
675
|
+
const body = await ctx.transport.request({
|
|
676
|
+
method: 'GET',
|
|
677
|
+
path,
|
|
678
|
+
query: includeQuery(include),
|
|
679
|
+
version: ctx.version,
|
|
680
|
+
signal,
|
|
681
|
+
});
|
|
682
|
+
return toItemResult(body);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
function writeBody(type: string, id: string | undefined, attributes: unknown, options: WriteOptions | undefined) {
|
|
686
|
+
const data: Record<string, unknown> = { type };
|
|
687
|
+
if (id !== undefined) data.id = id;
|
|
688
|
+
data.attributes = attributes ?? {};
|
|
689
|
+
if (options?.relationships) data.relationships = options.relationships;
|
|
690
|
+
return { data };
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/** @internal Runtime implementation of {@link Collection} and {@link CreatableCollection}. */
|
|
694
|
+
class CollectionImpl {
|
|
695
|
+
readonly path: string;
|
|
696
|
+
readonly #ctx: QueryContext;
|
|
697
|
+
readonly #edgeName: string;
|
|
698
|
+
readonly #edge: EdgeMeta;
|
|
699
|
+
readonly #state: CollectionState;
|
|
700
|
+
|
|
701
|
+
constructor(ctx: QueryContext, edgeName: string, edge: EdgeMeta, path: string, state: CollectionState = EMPTY_STATE) {
|
|
702
|
+
this.#ctx = ctx;
|
|
703
|
+
this.#edgeName = edgeName;
|
|
704
|
+
this.#edge = edge;
|
|
705
|
+
this.path = path;
|
|
706
|
+
this.#state = state;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
#with(patch: Partial<CollectionState>): CollectionImpl {
|
|
710
|
+
return new CollectionImpl(this.#ctx, this.#edgeName, this.#edge, this.path, { ...this.#state, ...patch });
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
where(field: string, opOrValue: unknown, value?: unknown): CollectionImpl {
|
|
714
|
+
assertNames('where field', [field]);
|
|
715
|
+
const given = arguments.length >= 3 ? value : opOrValue;
|
|
716
|
+
if (given === undefined || given === null) {
|
|
717
|
+
// Silently dropping the condition would widen the query (e.g. to every person).
|
|
718
|
+
throw new PcoError(`where("${field}") was given ${String(given)}; pass a value or leave the condition out.`);
|
|
719
|
+
}
|
|
720
|
+
if (arguments.length >= 3) {
|
|
721
|
+
const op = opOrValue;
|
|
722
|
+
if (typeof op !== 'string' || !WHERE_OPERATORS.has(op)) {
|
|
723
|
+
throw new PcoError(`Invalid where operator ${JSON.stringify(op)} for "${field}": expected gt, gte, lt, or lte.`);
|
|
724
|
+
}
|
|
725
|
+
return this.#with({ where: { ...this.#state.where, [`where[${field}][${op}]`]: value as QueryValue } });
|
|
726
|
+
}
|
|
727
|
+
return this.#with({ where: { ...this.#state.where, [`where[${field}]`]: opOrValue as QueryValue } });
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
filter(...names: string[]): CollectionImpl {
|
|
731
|
+
return this.#with({ filter: merge(this.#state.filter, assertNames('filter', names)) });
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
include(...names: string[]): CollectionImpl {
|
|
735
|
+
return this.#with({ include: merge(this.#state.include, assertNames('include', names)) });
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
order(...fields: string[]): CollectionImpl {
|
|
739
|
+
return this.#with({ order: assertNames('order', fields) });
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
perPage(n: number): CollectionImpl {
|
|
743
|
+
if (!Number.isInteger(n) || n < 1 || n > 100) {
|
|
744
|
+
throw new PcoError(`perPage must be an integer from 1 to 100 (got ${String(n)}).`);
|
|
745
|
+
}
|
|
746
|
+
return this.#with({ perPage: n });
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
offset(n: number): CollectionImpl {
|
|
750
|
+
if (!Number.isInteger(n) || n < 0) throw new PcoError(`offset must be a non-negative integer (got ${String(n)}).`);
|
|
751
|
+
return this.#with({ offset: n });
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
param(key: string, value: QueryValue): CollectionImpl {
|
|
755
|
+
if (typeof key !== 'string' || key === '') throw new PcoError('param key must be a non-empty string.');
|
|
756
|
+
return this.#with({ params: { ...this.#state.params, [key]: value } });
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
#query(overrides: { perPage?: number; offset?: number } = {}): Record<string, QueryValue> {
|
|
760
|
+
const s = this.#state;
|
|
761
|
+
const query: Record<string, QueryValue> = { ...s.where };
|
|
762
|
+
if (s.filter.length) query.filter = s.filter;
|
|
763
|
+
if (s.include.length) query.include = s.include;
|
|
764
|
+
if (s.order.length) query.order = s.order;
|
|
765
|
+
const perPage = overrides.perPage ?? s.perPage;
|
|
766
|
+
if (perPage !== undefined) query.per_page = perPage;
|
|
767
|
+
const offset = overrides.offset ?? s.offset;
|
|
768
|
+
if (offset !== undefined) query.offset = offset;
|
|
769
|
+
return Object.assign(query, s.params);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
async #fetch(query: Record<string, QueryValue> | undefined, path: string, signal?: AbortSignal) {
|
|
773
|
+
const body = await this.#ctx.transport.request({ method: 'GET', path, query, version: this.#ctx.version, signal });
|
|
774
|
+
return toListResult(body);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
list(options?: RequestOptions): Promise<ListResult<ProductSchema, string>> {
|
|
778
|
+
return this.#fetch(this.#query(), this.path, options?.signal);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
async *pages(options?: PagesOptions): AsyncGenerator<ListResult<ProductSchema, string>, void, undefined> {
|
|
782
|
+
const maxPages = options?.maxPages ?? Infinity;
|
|
783
|
+
if (maxPages <= 0) return;
|
|
784
|
+
const perPage = this.#state.perPage ?? 100;
|
|
785
|
+
let page = await this.#fetch(this.#query({ perPage }), this.path, options?.signal);
|
|
786
|
+
let count = 0;
|
|
787
|
+
const seen = new Set<string>();
|
|
788
|
+
for (;;) {
|
|
789
|
+
yield page;
|
|
790
|
+
if (++count >= maxPages || page.data.length === 0) return;
|
|
791
|
+
let next: Promise<ListResult<ProductSchema, string>> | undefined;
|
|
792
|
+
if (page.nextOffset !== undefined) {
|
|
793
|
+
const key = `offset:${page.nextOffset}`;
|
|
794
|
+
if (seen.has(key)) return;
|
|
795
|
+
seen.add(key);
|
|
796
|
+
next = this.#fetch(this.#query({ perPage, offset: page.nextOffset }), this.path, options?.signal);
|
|
797
|
+
} else if (typeof page.links.next === 'string' && page.links.next) {
|
|
798
|
+
const key = `link:${page.links.next}`;
|
|
799
|
+
if (seen.has(key)) return;
|
|
800
|
+
seen.add(key);
|
|
801
|
+
next = this.#fetch(undefined, page.links.next, options?.signal);
|
|
802
|
+
}
|
|
803
|
+
if (!next) return;
|
|
804
|
+
page = await next;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
async *all(options?: IterateOptions): AsyncGenerator<PcoResource, void, undefined> {
|
|
809
|
+
const max = options?.max ?? Infinity;
|
|
810
|
+
if (max <= 0) return;
|
|
811
|
+
let yielded = 0;
|
|
812
|
+
for await (const page of this.pages({ signal: options?.signal })) {
|
|
813
|
+
for (const resource of page.data) {
|
|
814
|
+
yield resource;
|
|
815
|
+
if (++yielded >= max) return;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
async toArray(options?: IterateOptions): Promise<PcoResource[]> {
|
|
821
|
+
const out: PcoResource[] = [];
|
|
822
|
+
for await (const resource of this.all(options)) out.push(resource);
|
|
823
|
+
return out;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
async first(options?: RequestOptions): Promise<PcoResource | undefined> {
|
|
827
|
+
const page = await this.#fetch(this.#query({ perPage: 1 }), this.path, options?.signal);
|
|
828
|
+
return page.data[0];
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
async count(options?: RequestOptions): Promise<number> {
|
|
832
|
+
const page = await this.#fetch(this.#query({ perPage: 1 }), this.path, options?.signal);
|
|
833
|
+
if (page.totalCount !== undefined) return page.totalCount;
|
|
834
|
+
// No total from the API: count by paging rather than guess from `meta.count` (the page size).
|
|
835
|
+
let total = 0;
|
|
836
|
+
for await (const _ of this.all(options)) total++;
|
|
837
|
+
return total;
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
async get(id: ResourceId, options?: GetOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
841
|
+
const url = itemPath(this.#ctx, this.#edge.vertex, this.path, assertId(id));
|
|
842
|
+
return getItem(this.#ctx, url, options?.include ?? this.#state.include, options?.signal);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
id(id: ResourceId): ItemRefImpl {
|
|
846
|
+
const value = assertId(id);
|
|
847
|
+
return createItemRef(this.#ctx, this.#edge.vertex, itemPath(this.#ctx, this.#edge.vertex, this.path, value), value);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
async create(attributes: unknown, options?: WriteOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
851
|
+
if (!this.#edge.canCreate) {
|
|
852
|
+
throw new PcoError(`Planning Center does not support creating resources at ${this.path} ("${this.#edgeName}").`);
|
|
853
|
+
}
|
|
854
|
+
const meta = vertexMeta(this.#ctx, this.#edge.vertex);
|
|
855
|
+
const body = await this.#ctx.transport.request({
|
|
856
|
+
method: 'POST',
|
|
857
|
+
path: this.path,
|
|
858
|
+
query: includeQuery(options?.include),
|
|
859
|
+
body: writeBody(meta.type, undefined, attributes, options),
|
|
860
|
+
version: this.#ctx.version,
|
|
861
|
+
signal: options?.signal,
|
|
862
|
+
});
|
|
863
|
+
return toItemResult(body);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/** @internal Runtime implementation of {@link SingleRef}. */
|
|
868
|
+
class SingleRefImpl {
|
|
869
|
+
readonly path: string;
|
|
870
|
+
readonly #ctx: QueryContext;
|
|
871
|
+
readonly #edge: EdgeMeta;
|
|
872
|
+
readonly #edgeName: string;
|
|
873
|
+
|
|
874
|
+
constructor(ctx: QueryContext, name: string, edge: EdgeMeta, path: string) {
|
|
875
|
+
this.#ctx = ctx;
|
|
876
|
+
this.#edge = edge;
|
|
877
|
+
this.#edgeName = name;
|
|
878
|
+
this.path = path;
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
get(options?: GetOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
882
|
+
return getItem(this.#ctx, this.path, options?.include, options?.signal);
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
async create(attributes: unknown, options?: WriteOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
886
|
+
if (!this.#edge.canCreate) {
|
|
887
|
+
throw new PcoError(`Planning Center does not support creating resources at ${this.path} ("${this.#edgeName}").`);
|
|
888
|
+
}
|
|
889
|
+
const meta = vertexMeta(this.#ctx, this.#edge.vertex);
|
|
890
|
+
const body = await this.#ctx.transport.request({
|
|
891
|
+
method: 'POST',
|
|
892
|
+
path: this.path,
|
|
893
|
+
query: includeQuery(options?.include),
|
|
894
|
+
body: writeBody(meta.type, undefined, attributes, options),
|
|
895
|
+
version: this.#ctx.version,
|
|
896
|
+
signal: options?.signal,
|
|
897
|
+
});
|
|
898
|
+
return toItemResult(body);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
/** @internal Runtime implementation of {@link ItemRef}. Edge getters live on per-vertex subclasses. */
|
|
903
|
+
class ItemRefImpl {
|
|
904
|
+
readonly path: string;
|
|
905
|
+
readonly #ctx: QueryContext;
|
|
906
|
+
readonly #vertex: string;
|
|
907
|
+
readonly #id: string;
|
|
908
|
+
|
|
909
|
+
constructor(ctx: QueryContext, vertex: string, path: string, id: string) {
|
|
910
|
+
this.#ctx = ctx;
|
|
911
|
+
this.#vertex = vertex;
|
|
912
|
+
this.path = path;
|
|
913
|
+
this.#id = id;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
get(options?: GetOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
917
|
+
return getItem(this.#ctx, this.path, options?.include, options?.signal);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async update(attributes: unknown, options?: WriteOptions): Promise<ItemResult<ProductSchema, string, PcoResource | null>> {
|
|
921
|
+
const meta = vertexMeta(this.#ctx, this.#vertex);
|
|
922
|
+
if (!meta.canUpdate) throw new PcoError(`Planning Center does not support updating ${meta.type} resources (${this.path}).`);
|
|
923
|
+
const body = await this.#ctx.transport.request({
|
|
924
|
+
method: 'PATCH',
|
|
925
|
+
path: this.path,
|
|
926
|
+
query: includeQuery(options?.include),
|
|
927
|
+
body: writeBody(meta.type, this.#id, attributes, options),
|
|
928
|
+
version: this.#ctx.version,
|
|
929
|
+
signal: options?.signal,
|
|
930
|
+
});
|
|
931
|
+
return toItemResult(body);
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
async delete(options?: RequestOptions): Promise<void> {
|
|
935
|
+
const meta = vertexMeta(this.#ctx, this.#vertex);
|
|
936
|
+
if (!meta.canDelete) throw new PcoError(`Planning Center does not support deleting ${meta.type} resources (${this.path}).`);
|
|
937
|
+
await this.#ctx.transport.request({ method: 'DELETE', path: this.path, version: this.#ctx.version, signal: options?.signal });
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
action(name: string, body?: unknown, options?: RequestOptions): Promise<unknown> {
|
|
941
|
+
const meta = vertexMeta(this.#ctx, this.#vertex);
|
|
942
|
+
if (typeof name !== 'string' || !meta.actions.includes(name)) {
|
|
943
|
+
return Promise.reject(new PcoError(
|
|
944
|
+
`Unknown action ${JSON.stringify(name)} for ${meta.type}. Known actions: ${meta.actions.join(', ') || 'none'}. ` +
|
|
945
|
+
'Use pco.request() for undocumented endpoints.',
|
|
946
|
+
));
|
|
947
|
+
}
|
|
948
|
+
return this.#ctx.transport.request({
|
|
949
|
+
method: 'POST',
|
|
950
|
+
path: `${this.path}/${name}`,
|
|
951
|
+
body,
|
|
952
|
+
version: this.#ctx.version,
|
|
953
|
+
signal: options?.signal,
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
edge(name: string): object {
|
|
958
|
+
const meta = vertexMeta(this.#ctx, this.#vertex);
|
|
959
|
+
const edge = Object.hasOwn(meta.edges, name) ? meta.edges[name] : undefined;
|
|
960
|
+
if (!edge) throw new PcoError(`${meta.type} has no edge "${name}".`);
|
|
961
|
+
return createEdgeBuilder(this.#ctx, name, edge, `${this.path}/${name}`);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Builds the builder for an edge at `path`.
|
|
967
|
+
* @internal
|
|
968
|
+
*/
|
|
969
|
+
export function createEdgeBuilder(ctx: QueryContext, name: string, edge: EdgeMeta, path: string): object {
|
|
970
|
+
return edge.many ? new CollectionImpl(ctx, name, edge, path) : new SingleRefImpl(ctx, name, edge, path);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* Defines a lazy getter on `proto` for each edge, skipping names that are already members of the
|
|
975
|
+
* prototype chain (methods such as `get`) and `reserved` names. Always reserve `then`: defining it
|
|
976
|
+
* would make the object thenable.
|
|
977
|
+
* @internal
|
|
978
|
+
*/
|
|
979
|
+
export function defineEdgeGetters<T extends object>(
|
|
980
|
+
proto: T,
|
|
981
|
+
names: Iterable<string>,
|
|
982
|
+
reserved: ReadonlySet<string>,
|
|
983
|
+
follow: (self: T, name: string) => unknown,
|
|
984
|
+
): void {
|
|
985
|
+
for (const name of names) {
|
|
986
|
+
if (reserved.has(name) || name in proto) continue;
|
|
987
|
+
Object.defineProperty(proto, name, {
|
|
988
|
+
configurable: true,
|
|
989
|
+
enumerable: false,
|
|
990
|
+
get(this: T) {
|
|
991
|
+
return follow(this, name);
|
|
992
|
+
},
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
const ITEM_RESERVED: ReadonlySet<string> = new Set<ItemReservedName>(['get', 'update', 'delete', 'action', 'edge', 'path', 'then']);
|
|
998
|
+
|
|
999
|
+
const itemClasses = new WeakMap<VertexMeta, typeof ItemRefImpl>();
|
|
1000
|
+
|
|
1001
|
+
function createItemRef(ctx: QueryContext, vertex: string, path: string, id: string): ItemRefImpl {
|
|
1002
|
+
const meta = vertexMeta(ctx, vertex);
|
|
1003
|
+
let cls = itemClasses.get(meta);
|
|
1004
|
+
if (!cls) {
|
|
1005
|
+
cls = class extends ItemRefImpl {};
|
|
1006
|
+
Object.defineProperty(cls, 'name', { value: `ItemRef(${meta.type})` });
|
|
1007
|
+
defineEdgeGetters<ItemRefImpl>(cls.prototype, Object.keys(meta.edges), ITEM_RESERVED, (self, name) => self.edge(name));
|
|
1008
|
+
itemClasses.set(meta, cls);
|
|
1009
|
+
}
|
|
1010
|
+
return new cls(ctx, vertex, path, id);
|
|
1011
|
+
}
|