@knocklabs/client 0.14.11 → 0.15.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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.15.1
4
+
5
+ ### Patch Changes
6
+
7
+ - befd7b9: fix: when reseting the feed, real-time notifications stop working
8
+
9
+ ## 0.15.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 2a0b3e2: Adds support for inline user `identify` calls when authenticating a user via the Knock client.
14
+ You can now pass a `user` object, for example `{ id: "123" }`, directly to the `authenticate` function.
15
+ Additional properties can also be included to update the user record, such as `{ id: "123", name: "Knock" }`.
16
+
17
+ This update also applies to `KnockProvider`, where you can now pass a `user` prop instead of a `userId` prop to achieve the same behavior.
18
+
3
19
  ## 0.14.11
4
20
 
5
21
  ### Patch Changes
@@ -1,2 +1,2 @@
1
- "use strict";var f=Object.defineProperty;var g=(s,t,e)=>t in s?f(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var l=(s,t,e)=>g(s,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=require("@tanstack/store"),d=require("../../networkStatus.js"),S=require("./utils.js");function m(s){const t=S.deduplicateItems(s);return S.sortItems(t)}const w={shouldSetPage:!0,shouldAppend:!1},u={items:[],metadata:{total_count:0,unread_count:0,unseen_count:0},pageInfo:{before:null,after:null,page_size:50},loading:!1,networkStatus:d.NetworkStatus.ready,setResult:()=>{},setMetadata:()=>{},setNetworkStatus:()=>{},resetStore:()=>{},setItemAttrs:()=>{}},k=()=>{const s=new p.Store(u);return s.setState(t=>({...t,networkStatus:d.NetworkStatus.ready,loading:!1,setNetworkStatus:e=>s.setState(r=>({...r,networkStatus:e,loading:e===d.NetworkStatus.loading})),setResult:({entries:e,meta:r,page_info:i},o=w)=>s.setState(a=>{const n=o.shouldAppend?m(a.items.concat(e)):e;return{...a,items:n,metadata:r,pageInfo:o.shouldSetPage?i:a.pageInfo,loading:!1,networkStatus:d.NetworkStatus.ready}}),setMetadata:e=>s.setState(r=>({...r,metadata:e})),resetStore:(e=u.metadata)=>s.setState(()=>({...u,metadata:e})),setItemAttrs:(e,r)=>{const i=e.reduce((o,a)=>({...o,[a]:r}),{});return s.setState(o=>{const a=o.items.map(n=>i[n.id]?{...n,...i[n.id]}:n);return{...o,items:a}})}})),s};class c{constructor(t){l(this,"store");this.store=t}getState(){return this.store.state}setState(t){this.store.setState(typeof t=="function"?t:()=>t)}getInitialState(){return u}subscribe(t){return this.store.subscribe(e=>t(e.currentVal))}}function I(){const s=k();return new c(s)}exports.FeedStore=c;exports.default=I;exports.initialStoreState=u;
1
+ "use strict";var f=Object.defineProperty;var g=(t,e,s)=>e in t?f(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var d=(t,e,s)=>g(t,typeof e!="symbol"?e+"":e,s);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("@tanstack/store"),S=require("../../networkStatus.js"),l=require("./utils.js");function p(t){const e=l.deduplicateItems(t);return l.sortItems(e)}const w={shouldSetPage:!0,shouldAppend:!1},u={items:[],metadata:{total_count:0,unread_count:0,unseen_count:0},pageInfo:{before:null,after:null,page_size:50},loading:!1,networkStatus:S.NetworkStatus.ready,setResult:()=>{},setMetadata:()=>{},setNetworkStatus:()=>{},resetStore:()=>{},setItemAttrs:()=>{}},k=()=>{const t=new m.Store(u);return t.setState(e=>({...e,networkStatus:S.NetworkStatus.ready,loading:!1,setNetworkStatus:s=>t.setState(r=>({...r,networkStatus:s,loading:s===S.NetworkStatus.loading})),setResult:({entries:s,meta:r,page_info:i},a=w)=>t.setState(o=>{const n=a.shouldAppend?p(o.items.concat(s)):s;return{...o,items:n,metadata:r,pageInfo:a.shouldSetPage?i:o.pageInfo,loading:!1,networkStatus:S.NetworkStatus.ready}}),setMetadata:s=>t.setState(r=>({...r,metadata:s})),resetStore:(s=u.metadata)=>t.setState(()=>({...u,setResult:t.state.setResult,setMetadata:t.state.setMetadata,setNetworkStatus:t.state.setNetworkStatus,resetStore:t.state.resetStore,setItemAttrs:t.state.setItemAttrs,metadata:s})),setItemAttrs:(s,r)=>{const i=s.reduce((a,o)=>({...a,[o]:r}),{});return t.setState(a=>{const o=a.items.map(n=>i[n.id]?{...n,...i[n.id]}:n);return{...a,items:o}})}})),t};class c{constructor(e){d(this,"store");this.store=e}getState(){return this.store.state}setState(e){this.store.setState(typeof e=="function"?e:()=>e)}getInitialState(){return u}subscribe(e){return this.store.subscribe(s=>e(s.currentVal))}}function I(){const t=k();return new c(t)}exports.FeedStore=c;exports.default=I;exports.initialStoreState=u;
2
2
  //# sourceMappingURL=store.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem, FeedMetadata, FeedResponse } from \"./interfaces\";\nimport { FeedStoreState, StoreFeedResultOptions } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nexport const initialStoreState: FeedStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n loading: false,\n networkStatus: NetworkStatus.ready,\n setResult: () => {},\n setMetadata: () => {},\n setNetworkStatus: () => {},\n resetStore: () => {},\n setItemAttrs: () => {},\n};\n\n/**\n * Initalize the store with tanstack store so it can be used within our\n * FeedStore class. We do this seperately so that we have the ability to\n * change which store library we use in the future if need be.\n */\nconst initalizeStore = () => {\n const store = new Store(initialStoreState);\n\n store.setState((state) => ({\n ...state,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n store.setState((state) => ({\n ...state,\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info }: FeedResponse,\n options: StoreFeedResultOptions = defaultSetResultOptions,\n ) =>\n store.setState((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n ...state,\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata: FeedMetadata) =>\n store.setState((state) => ({ ...state, metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n store.setState(() => ({ ...initialStoreState, metadata })),\n\n setItemAttrs: (itemIds: Array<string>, attrs: object) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return store.setState((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { ...state, items };\n });\n },\n }));\n\n return store;\n};\n\n/**\n * The FeedStore class is a wrapper for our store solution that's\n * based on the same shape as zustand. This wrapping class allows\n * us to maintain backwards compatibility with the zustand model\n * while still allowing us to utilize tanstack store for the\n * underlying store solution.\n */\nexport class FeedStore {\n store: Store<FeedStoreState>;\n\n constructor(store: Store<FeedStoreState>) {\n this.store = store;\n }\n\n getState() {\n return this.store.state;\n }\n\n setState(\n updater: ((state: FeedStoreState) => FeedStoreState) | FeedStoreState,\n ) {\n this.store.setState(\n typeof updater === \"function\" ? updater : () => updater,\n );\n }\n\n getInitialState() {\n return initialStoreState;\n }\n\n subscribe(listener: (state: FeedStoreState) => void) {\n return this.store.subscribe((state) => listener(state.currentVal));\n }\n}\n\nexport default function createStore() {\n const store = initalizeStore();\n return new FeedStore(store);\n}\n"],"names":["processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","NetworkStatus","initalizeStore","store","Store","state","networkStatus","entries","meta","page_info","options","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item","FeedStore","__publicField","updater","listener","createStore"],"mappings":"+WASA,SAASA,EAAaC,EAAmB,CACjC,MAAAC,EAAUC,mBAAiBF,CAAK,EAG/B,OAFQG,YAAUF,CAAO,CAGlC,CAEA,MAAMG,EAA0B,CAC9B,cAAe,GACf,aAAc,EAChB,EAEaC,EAAoC,CAC/C,MAAO,CAAC,EACR,SAAU,CACR,YAAa,EACb,aAAc,EACd,aAAc,CAChB,EACA,SAAU,CACR,OAAQ,KACR,MAAO,KACP,UAAW,EACb,EACA,QAAS,GACT,cAAeC,EAAc,cAAA,MAC7B,UAAW,IAAM,CAAC,EAClB,YAAa,IAAM,CAAC,EACpB,iBAAkB,IAAM,CAAC,EACzB,WAAY,IAAM,CAAC,EACnB,aAAc,IAAM,CAAA,CACtB,EAOMC,EAAiB,IAAM,CACrB,MAAAC,EAAQ,IAAIC,EAAA,MAAMJ,CAAiB,EAEnCG,OAAAA,EAAA,SAAUE,IAAW,CACzB,GAAGA,EAEH,cAAeJ,EAAc,cAAA,MAC7B,QAAS,GACT,iBAAmBK,GACjBH,EAAM,SAAUE,IAAW,CACzB,GAAGA,EAAA,cACHC,EACA,QAASA,IAAkBL,gBAAc,OAAA,EACzC,EAEJ,UAAW,CACT,CAAE,QAAAM,EAAS,KAAAC,EAAM,UAAAC,CAAA,EACjBC,EAAkCX,IAElCI,EAAM,SAAUE,GAAU,CAElB,MAAAV,EAAQe,EAAQ,aAClBhB,EAAaW,EAAM,MAAM,OAAOE,CAAkC,CAAC,EACnEA,EAEG,MAAA,CACL,GAAGF,EACH,MAAAV,EACA,SAAUa,EACV,SAAUE,EAAQ,cAAgBD,EAAYJ,EAAM,SACpD,QAAS,GACT,cAAeJ,EAAAA,cAAc,KAC/B,CAAA,CACD,EAEH,YAAcU,GACZR,EAAM,SAAUE,IAAW,CAAE,GAAGA,EAAO,SAAAM,CAAA,EAAW,EAEpD,WAAY,CAACA,EAAWX,EAAkB,WACxCG,EAAM,SAAS,KAAO,CAAE,GAAGH,EAAmB,SAAAW,CAAW,EAAA,EAE3D,aAAc,CAACC,EAAwBC,IAAkB,CAEvD,MAAMC,EAA2CF,EAAQ,OACvD,CAACG,EAAKC,KAAY,CAAE,GAAGD,EAAK,CAACC,CAAM,EAAGH,IACtC,CAAA,CACF,EAEO,OAAAV,EAAM,SAAUE,GAAU,CAC/B,MAAMV,EAAQU,EAAM,MAAM,IAAKY,GACzBH,EAAeG,EAAK,EAAE,EACjB,CAAE,GAAGA,EAAM,GAAGH,EAAeG,EAAK,EAAE,CAAE,EAGxCA,CACR,EAEM,MAAA,CAAE,GAAGZ,EAAO,MAAAV,CAAM,CAAA,CAC1B,CAAA,CACH,EACA,EAEKQ,CACT,EASO,MAAMe,CAAU,CAGrB,YAAYf,EAA8B,CAF1CgB,EAAA,cAGE,KAAK,MAAQhB,CAAA,CAGf,UAAW,CACT,OAAO,KAAK,MAAM,KAAA,CAGpB,SACEiB,EACA,CACA,KAAK,MAAM,SACT,OAAOA,GAAY,WAAaA,EAAU,IAAMA,CAClD,CAAA,CAGF,iBAAkB,CACT,OAAApB,CAAA,CAGT,UAAUqB,EAA2C,CAC5C,OAAA,KAAK,MAAM,UAAWhB,GAAUgB,EAAShB,EAAM,UAAU,CAAC,CAAA,CAErE,CAEA,SAAwBiB,GAAc,CACpC,MAAMnB,EAAQD,EAAe,EACtB,OAAA,IAAIgB,EAAUf,CAAK,CAC5B"}
1
+ {"version":3,"file":"store.js","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem, FeedMetadata, FeedResponse } from \"./interfaces\";\nimport { FeedStoreState, StoreFeedResultOptions } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nexport const initialStoreState: FeedStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n loading: false,\n networkStatus: NetworkStatus.ready,\n setResult: () => {},\n setMetadata: () => {},\n setNetworkStatus: () => {},\n resetStore: () => {},\n setItemAttrs: () => {},\n};\n\n/**\n * Initalize the store with tanstack store so it can be used within our\n * FeedStore class. We do this seperately so that we have the ability to\n * change which store library we use in the future if need be.\n */\nconst initalizeStore = () => {\n const store = new Store(initialStoreState);\n\n store.setState((state) => ({\n ...state,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n store.setState((state) => ({\n ...state,\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info }: FeedResponse,\n options: StoreFeedResultOptions = defaultSetResultOptions,\n ) =>\n store.setState((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n ...state,\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata: FeedMetadata) =>\n store.setState((state) => ({ ...state, metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n store.setState(() => ({\n ...initialStoreState,\n setResult: store.state.setResult,\n setMetadata: store.state.setMetadata,\n setNetworkStatus: store.state.setNetworkStatus,\n resetStore: store.state.resetStore,\n setItemAttrs: store.state.setItemAttrs,\n metadata,\n })),\n\n setItemAttrs: (itemIds: Array<string>, attrs: object) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return store.setState((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { ...state, items };\n });\n },\n }));\n\n return store;\n};\n\n/**\n * The FeedStore class is a wrapper for our store solution that's\n * based on the same shape as zustand. This wrapping class allows\n * us to maintain backwards compatibility with the zustand model\n * while still allowing us to utilize tanstack store for the\n * underlying store solution.\n */\nexport class FeedStore {\n store: Store<FeedStoreState>;\n\n constructor(store: Store<FeedStoreState>) {\n this.store = store;\n }\n\n getState() {\n return this.store.state;\n }\n\n setState(\n updater: ((state: FeedStoreState) => FeedStoreState) | FeedStoreState,\n ) {\n this.store.setState(\n typeof updater === \"function\" ? updater : () => updater,\n );\n }\n\n getInitialState() {\n return initialStoreState;\n }\n\n subscribe(listener: (state: FeedStoreState) => void) {\n return this.store.subscribe((state) => listener(state.currentVal));\n }\n}\n\nexport default function createStore() {\n const store = initalizeStore();\n return new FeedStore(store);\n}\n"],"names":["processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","NetworkStatus","initalizeStore","store","Store","state","networkStatus","entries","meta","page_info","options","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item","FeedStore","__publicField","updater","listener","createStore"],"mappings":"+WASA,SAASA,EAAaC,EAAmB,CACjC,MAAAC,EAAUC,mBAAiBF,CAAK,EAG/B,OAFQG,YAAUF,CAAO,CAGlC,CAEA,MAAMG,EAA0B,CAC9B,cAAe,GACf,aAAc,EAChB,EAEaC,EAAoC,CAC/C,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2CF,EAAQ,OACvD,CAACG,EAAKC,KAAY,CAAE,GAAGD,EAAK,CAACC,CAAM,EAAGH,IACtC,CAAA,CACF,EAEO,OAAAV,EAAM,SAAUE,GAAU,CAC/B,MAAMV,EAAQU,EAAM,MAAM,IAAKY,GACzBH,EAAeG,EAAK,EAAE,EACjB,CAAE,GAAGA,EAAM,GAAGH,EAAeG,EAAK,EAAE,CAAE,EAGxCA,CACR,EAEM,MAAA,CAAE,GAAGZ,EAAO,MAAAV,CAAM,CAAA,CAC1B,CAAA,CACH,EACA,EAEKQ,CACT,EASO,MAAMe,CAAU,CAGrB,YAAYf,EAA8B,CAF1CgB,EAAA,cAGE,KAAK,MAAQhB,CAAA,CAGf,UAAW,CACT,OAAO,KAAK,MAAM,KAAA,CAGpB,SACEiB,EACA,CACA,KAAK,MAAM,SACT,OAAOA,GAAY,WAAaA,EAAU,IAAMA,CAClD,CAAA,CAGF,iBAAkB,CACT,OAAApB,CAAA,CAGT,UAAUqB,EAA2C,CAC5C,OAAA,KAAK,MAAM,UAAWhB,GAAUgB,EAAShB,EAAM,UAAU,CAAC,CAAA,CAErE,CAEA,SAAwBiB,GAAc,CACpC,MAAMnB,EAAQD,EAAe,EACtB,OAAA,IAAIgB,EAAUf,CAAK,CAC5B"}
package/dist/cjs/knock.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var u=Object.defineProperty;var c=(n,e,t)=>e in n?u(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var i=(n,e,t)=>c(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const l=require("jwt-decode"),d=require("./api.js"),f=require("./clients/feed/index.js"),p=require("./clients/messages/index.js"),k=require("./clients/ms-teams/index.js"),T=require("./clients/objects/index.js"),w=require("./clients/preferences/index.js"),g=require("./clients/slack/index.js"),x=require("./clients/users/index.js"),C="https://api.knock.app";class m{constructor(e,t={}){i(this,"host");i(this,"apiClient",null);i(this,"userId");i(this,"userToken");i(this,"logLevel");i(this,"tokenExpirationTimer",null);i(this,"feeds",new f.default(this));i(this,"objects",new T.default(this));i(this,"preferences",new w.default(this));i(this,"slack",new g.default(this));i(this,"msTeams",new k.default(this));i(this,"user",new x.default(this));i(this,"messages",new p.default(this));if(this.apiKey=e,this.host=t.host||C,this.logLevel=t.logLevel,this.log("Initialized Knock instance"),this.apiKey&&this.apiKey.startsWith("sk_"))throw new Error("[Knock] You are using your secret API key on the client. Please use the public key.")}client(){return this.apiClient||(this.apiClient=this.createApiClient()),this.apiClient}authenticate(e,t,s){let r=!1;this.apiClient&&(this.userId!==e||this.userToken!==t)&&(this.log("userId or userToken changed; reinitializing connections"),this.feeds.teardownInstances(),this.teardown(),r=!0),this.userId=e,this.userToken=t,this.log(`Authenticated with userId ${e}`),this.userToken&&(s==null?void 0:s.onUserTokenExpiring)instanceof Function&&this.maybeScheduleUserTokenExpiration(s.onUserTokenExpiring,s.timeBeforeExpirationInMs),r&&(this.apiClient=this.createApiClient(),this.feeds.reinitializeInstances(),this.log("Reinitialized real-time connections"))}failIfNotAuthenticated(){if(!this.isAuthenticated())throw new Error("Not authenticated. Please call `authenticate` first.")}isAuthenticated(e=!1){return e?!!(this.userId&&this.userToken):!!this.userId}teardown(){var e;this.tokenExpirationTimer&&clearTimeout(this.tokenExpirationTimer),(e=this.apiClient)!=null&&e.socket&&this.apiClient.socket.isConnected()&&this.apiClient.socket.disconnect()}log(e,t=!1){(this.logLevel==="debug"||t)&&console.log(`[Knock] ${e}`)}createApiClient(){return new d.default({apiKey:this.apiKey,host:this.host,userToken:this.userToken})}async maybeScheduleUserTokenExpiration(e,t=3e4){if(!this.userToken)return;const s=l.jwtDecode(this.userToken),r=(s.exp??0)*1e3,o=Date.now();if(r&&r>o){const h=r-t-o;this.tokenExpirationTimer=setTimeout(async()=>{const a=await e(this.userToken,s);typeof a=="string"&&this.authenticate(this.userId,a,{onUserTokenExpiring:e,timeBeforeExpirationInMs:t})},h)}}}exports.default=m;
1
+ "use strict";var h=Object.defineProperty;var r=(s,e,t)=>e in s?h(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var i=(s,e,t)=>r(s,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const d=require("jwt-decode"),f=require("./api.js"),k=require("./clients/feed/index.js"),T=require("./clients/messages/index.js"),g=require("./clients/ms-teams/index.js"),w=require("./clients/objects/index.js"),p=require("./clients/preferences/index.js"),x=require("./clients/slack/index.js"),y=require("./clients/users/index.js"),m="https://api.knock.app";class C{constructor(e,t={}){i(this,"host");i(this,"apiClient",null);i(this,"userId");i(this,"userToken");i(this,"logLevel");i(this,"tokenExpirationTimer",null);i(this,"feeds",new k.default(this));i(this,"objects",new w.default(this));i(this,"preferences",new p.default(this));i(this,"slack",new x.default(this));i(this,"msTeams",new g.default(this));i(this,"user",new y.default(this));i(this,"messages",new T.default(this));if(this.apiKey=e,this.host=t.host||m,this.logLevel=t.logLevel,this.log("Initialized Knock instance"),this.apiKey&&this.apiKey.startsWith("sk_"))throw new Error("[Knock] You are using your secret API key on the client. Please use the public key.")}client(){return this.apiClient||(this.apiClient=this.createApiClient()),this.apiClient}authenticate(e,t,n){let o=!1;const c=this.apiClient,a=this.getUserId(e);if(c&&(this.userId!==a||this.userToken!==t)&&(this.log("userId or userToken changed; reinitializing connections"),this.feeds.teardownInstances(),this.teardown(),o=!0),this.userId=a,this.userToken=t,this.log(`Authenticated with userId ${a}`),this.userToken&&(n==null?void 0:n.onUserTokenExpiring)instanceof Function&&this.maybeScheduleUserTokenExpiration(n.onUserTokenExpiring,n.timeBeforeExpirationInMs),o&&(this.apiClient=this.createApiClient(),this.feeds.reinitializeInstances(),this.log("Reinitialized real-time connections")),typeof e=="object"&&(e!=null&&e.id)){const{id:l,...u}=e;this.user.identify(u)}}failIfNotAuthenticated(){if(!this.isAuthenticated())throw new Error("Not authenticated. Please call `authenticate` first.")}isAuthenticated(e=!1){return e?!!(this.userId&&this.userToken):!!this.userId}teardown(){var e;this.tokenExpirationTimer&&clearTimeout(this.tokenExpirationTimer),(e=this.apiClient)!=null&&e.socket&&this.apiClient.socket.isConnected()&&this.apiClient.socket.disconnect()}log(e,t=!1){(this.logLevel==="debug"||t)&&console.log(`[Knock] ${e}`)}createApiClient(){return new f.default({apiKey:this.apiKey,host:this.host,userToken:this.userToken})}async maybeScheduleUserTokenExpiration(e,t=3e4){if(!this.userToken)return;const n=d.jwtDecode(this.userToken),o=(n.exp??0)*1e3,c=Date.now();if(o&&o>c){const a=o-t-c;this.tokenExpirationTimer=setTimeout(async()=>{const l=await e(this.userToken,n);typeof l=="string"&&this.authenticate(this.userId,l,{onUserTokenExpiring:e,timeBeforeExpirationInMs:t})},a)}}getUserId(e){if(typeof e=="string"||!e)return e;if(e!=null&&e.id)return e.id;throw new Error("`user` object must contain an `id` property")}}exports.default=C;
2
2
  //# sourceMappingURL=knock.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"knock.js","sources":["../../src/knock.ts"],"sourcesContent":["import { jwtDecode } from \"jwt-decode\";\n\nimport ApiClient from \"./api\";\nimport FeedClient from \"./clients/feed\";\nimport MessageClient from \"./clients/messages\";\nimport MsTeamsClient from \"./clients/ms-teams\";\nimport ObjectClient from \"./clients/objects\";\nimport Preferences from \"./clients/preferences\";\nimport SlackClient from \"./clients/slack\";\nimport UserClient from \"./clients/users\";\nimport {\n AuthenticateOptions,\n KnockOptions,\n LogLevel,\n UserTokenExpiringCallback,\n} from \"./interfaces\";\n\nconst DEFAULT_HOST = \"https://api.knock.app\";\n\nclass Knock {\n public host: string;\n private apiClient: ApiClient | null = null;\n public userId: string | undefined | null;\n public userToken?: string;\n public logLevel?: LogLevel;\n private tokenExpirationTimer: ReturnType<typeof setTimeout> | null = null;\n readonly feeds = new FeedClient(this);\n readonly objects = new ObjectClient(this);\n readonly preferences = new Preferences(this);\n readonly slack = new SlackClient(this);\n readonly msTeams = new MsTeamsClient(this);\n readonly user = new UserClient(this);\n readonly messages = new MessageClient(this);\n\n constructor(\n readonly apiKey: string,\n options: KnockOptions = {},\n ) {\n this.host = options.host || DEFAULT_HOST;\n this.logLevel = options.logLevel;\n\n this.log(\"Initialized Knock instance\");\n\n // Fail loudly if we're using the wrong API key\n if (this.apiKey && this.apiKey.startsWith(\"sk_\")) {\n throw new Error(\n \"[Knock] You are using your secret API key on the client. Please use the public key.\",\n );\n }\n }\n\n client() {\n // Initiate a new API client if we don't have one yet\n if (!this.apiClient) {\n this.apiClient = this.createApiClient();\n }\n\n return this.apiClient;\n }\n\n /*\n Authenticates the current user. In non-sandbox environments\n the userToken must be specified.\n */\n authenticate(\n userId: Knock[\"userId\"],\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ) {\n let reinitializeApi = false;\n const currentApiClient = this.apiClient;\n\n // If we've previously been initialized and the values have now changed, then we\n // need to reinitialize any stateful connections we have\n if (\n currentApiClient &&\n (this.userId !== userId || this.userToken !== userToken)\n ) {\n this.log(\"userId or userToken changed; reinitializing connections\");\n this.feeds.teardownInstances();\n this.teardown();\n reinitializeApi = true;\n }\n\n this.userId = userId;\n this.userToken = userToken;\n\n this.log(`Authenticated with userId ${userId}`);\n\n if (this.userToken && options?.onUserTokenExpiring instanceof Function) {\n this.maybeScheduleUserTokenExpiration(\n options.onUserTokenExpiring,\n options.timeBeforeExpirationInMs,\n );\n }\n\n // If we get the signal to reinitialize the api client, then we want to create a new client\n // and the reinitialize any existing feed real-time connections we have so everything continues\n // to work with the new credentials we've been given\n if (reinitializeApi) {\n this.apiClient = this.createApiClient();\n this.feeds.reinitializeInstances();\n this.log(\"Reinitialized real-time connections\");\n }\n\n return;\n }\n\n failIfNotAuthenticated() {\n if (!this.isAuthenticated()) {\n throw new Error(\"Not authenticated. Please call `authenticate` first.\");\n }\n }\n\n /*\n Returns whether or this Knock instance is authenticated. Passing `true` will check the presence\n of the userToken as well.\n */\n isAuthenticated(checkUserToken = false) {\n return checkUserToken ? !!(this.userId && this.userToken) : !!this.userId;\n }\n\n // Used to teardown any connected instances\n teardown() {\n if (this.tokenExpirationTimer) {\n clearTimeout(this.tokenExpirationTimer);\n }\n if (this.apiClient?.socket && this.apiClient.socket.isConnected()) {\n this.apiClient.socket.disconnect();\n }\n }\n\n log(message: string, force = false) {\n if (this.logLevel === \"debug\" || force) {\n console.log(`[Knock] ${message}`);\n }\n }\n\n /**\n * Initiates an API client\n */\n private createApiClient() {\n return new ApiClient({\n apiKey: this.apiKey,\n host: this.host,\n userToken: this.userToken,\n });\n }\n\n private async maybeScheduleUserTokenExpiration(\n callbackFn: UserTokenExpiringCallback,\n timeBeforeExpirationInMs: number = 30_000,\n ) {\n if (!this.userToken) return;\n\n const decoded = jwtDecode(this.userToken);\n const expiresAtMs = (decoded.exp ?? 0) * 1000;\n const nowMs = Date.now();\n\n // Expiration is in the future\n if (expiresAtMs && expiresAtMs > nowMs) {\n // Check how long until the token should be regenerated\n // | ----------------- | ----------------------- |\n // ^ now ^ expiration offset ^ expires at\n const msInFuture = expiresAtMs - timeBeforeExpirationInMs - nowMs;\n\n this.tokenExpirationTimer = setTimeout(async () => {\n const newToken = await callbackFn(this.userToken as string, decoded);\n\n // Reauthenticate which will handle reinitializing sockets\n if (typeof newToken === \"string\") {\n this.authenticate(this.userId!, newToken, {\n onUserTokenExpiring: callbackFn,\n timeBeforeExpirationInMs: timeBeforeExpirationInMs,\n });\n }\n }, msInFuture);\n }\n }\n}\n\nexport default Knock;\n"],"names":["DEFAULT_HOST","Knock","apiKey","options","__publicField","FeedClient","ObjectClient","Preferences","SlackClient","MsTeamsClient","UserClient","MessageClient","userId","userToken","reinitializeApi","checkUserToken","_a","message","force","ApiClient","callbackFn","timeBeforeExpirationInMs","decoded","jwtDecode","expiresAtMs","nowMs","msInFuture","newToken"],"mappings":"2lBAiBMA,EAAe,wBAErB,MAAMC,CAAM,CAeV,YACWC,EACTC,EAAwB,GACxB,CAjBKC,EAAA,aACCA,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CAC/B,OAAAA,EAAiB,CAAC,EAAE,KAAK,QAAU,KAAK,WAAa,CAAC,CAAC,KAAK,MAAA,CAIrE,UAAW,OACL,KAAK,sBACP,aAAa,KAAK,oBAAoB,GAEpCC,EAAA,KAAK,YAAL,MAAAA,EAAgB,QAAU,KAAK,UAAU,OAAO,eAC7C,KAAA,UAAU,OAAO,WAAW,CACnC,CAGF,IAAIC,EAAiBC,EAAQ,GAAO,EAC9B,KAAK,WAAa,SAAWA,IACvB,QAAA,IAAI,WAAWD,CAAO,EAAE,CAClC,CAMM,iBAAkB,CACxB,OAAO,IAAIE,EAAAA,QAAU,CACnB,OAAQ,KAAK,OACb,KAAM,KAAK,KACX,UAAW,KAAK,SAAA,CACjB,CAAA,CAGH,MAAc,iCACZC,EACAC,EAAmC,IACnC,CACI,GAAA,CAAC,KAAK,UAAW,OAEf,MAAAC,EAAUC,EAAAA,UAAU,KAAK,SAAS,EAClCC,GAAeF,EAAQ,KAAO,GAAK,IACnCG,EAAQ,KAAK,IAAI,EAGnB,GAAAD,GAAeA,EAAcC,EAAO,CAIhC,MAAAC,EAAaF,EAAcH,EAA2BI,EAEvD,KAAA,qBAAuB,WAAW,SAAY,CACjD,MAAME,EAAW,MAAMP,EAAW,KAAK,UAAqBE,CAAO,EAG/D,OAAOK,GAAa,UACjB,KAAA,aAAa,KAAK,OAASA,EAAU,CACxC,oBAAqBP,EACrB,yBAAAC,CAAA,CACD,GAEFK,CAAU,CAAA,CACf,CAEJ"}
1
+ {"version":3,"file":"knock.js","sources":["../../src/knock.ts"],"sourcesContent":["import { jwtDecode } from \"jwt-decode\";\n\nimport ApiClient from \"./api\";\nimport FeedClient from \"./clients/feed\";\nimport MessageClient from \"./clients/messages\";\nimport MsTeamsClient from \"./clients/ms-teams\";\nimport ObjectClient from \"./clients/objects\";\nimport Preferences from \"./clients/preferences\";\nimport SlackClient from \"./clients/slack\";\nimport UserClient from \"./clients/users\";\nimport {\n AuthenticateOptions,\n KnockOptions,\n LogLevel,\n UserId,\n UserIdOrUserWithProperties,\n UserTokenExpiringCallback,\n} from \"./interfaces\";\n\nconst DEFAULT_HOST = \"https://api.knock.app\";\n\nclass Knock {\n public host: string;\n private apiClient: ApiClient | null = null;\n public userId: string | undefined | null;\n public userToken?: string;\n public logLevel?: LogLevel;\n private tokenExpirationTimer: ReturnType<typeof setTimeout> | null = null;\n readonly feeds = new FeedClient(this);\n readonly objects = new ObjectClient(this);\n readonly preferences = new Preferences(this);\n readonly slack = new SlackClient(this);\n readonly msTeams = new MsTeamsClient(this);\n readonly user = new UserClient(this);\n readonly messages = new MessageClient(this);\n\n constructor(\n readonly apiKey: string,\n options: KnockOptions = {},\n ) {\n this.host = options.host || DEFAULT_HOST;\n this.logLevel = options.logLevel;\n\n this.log(\"Initialized Knock instance\");\n\n // Fail loudly if we're using the wrong API key\n if (this.apiKey && this.apiKey.startsWith(\"sk_\")) {\n throw new Error(\n \"[Knock] You are using your secret API key on the client. Please use the public key.\",\n );\n }\n }\n\n client() {\n // Initiate a new API client if we don't have one yet\n if (!this.apiClient) {\n this.apiClient = this.createApiClient();\n }\n\n return this.apiClient;\n }\n\n /**\n * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.\n * Please pass a `user` object instead containing an `id` value.\n * example:\n * ```ts\n * knock.authenticate({ id: \"user_123\" });\n * ```\n */\n authenticate(\n userIdOrUserWithProperties: UserId,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ): never;\n authenticate(\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ): void;\n authenticate(\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ) {\n let reinitializeApi = false;\n const currentApiClient = this.apiClient;\n const userId = this.getUserId(userIdOrUserWithProperties);\n\n // If we've previously been initialized and the values have now changed, then we\n // need to reinitialize any stateful connections we have\n if (\n currentApiClient &&\n (this.userId !== userId || this.userToken !== userToken)\n ) {\n this.log(\"userId or userToken changed; reinitializing connections\");\n this.feeds.teardownInstances();\n this.teardown();\n reinitializeApi = true;\n }\n\n this.userId = userId;\n this.userToken = userToken;\n\n this.log(`Authenticated with userId ${userId}`);\n\n if (this.userToken && options?.onUserTokenExpiring instanceof Function) {\n this.maybeScheduleUserTokenExpiration(\n options.onUserTokenExpiring,\n options.timeBeforeExpirationInMs,\n );\n }\n\n // If we get the signal to reinitialize the api client, then we want to create a new client\n // and the reinitialize any existing feed real-time connections we have so everything continues\n // to work with the new credentials we've been given\n if (reinitializeApi) {\n this.apiClient = this.createApiClient();\n this.feeds.reinitializeInstances();\n this.log(\"Reinitialized real-time connections\");\n }\n\n // Inline identify the user if we've been given an object with an id.\n if (\n typeof userIdOrUserWithProperties === \"object\" &&\n userIdOrUserWithProperties?.id\n ) {\n const { id, ...properties } = userIdOrUserWithProperties;\n this.user.identify(properties);\n }\n\n return;\n }\n\n failIfNotAuthenticated() {\n if (!this.isAuthenticated()) {\n throw new Error(\"Not authenticated. Please call `authenticate` first.\");\n }\n }\n\n /*\n Returns whether or this Knock instance is authenticated. Passing `true` will check the presence\n of the userToken as well.\n */\n isAuthenticated(checkUserToken = false) {\n return checkUserToken ? !!(this.userId && this.userToken) : !!this.userId;\n }\n\n // Used to teardown any connected instances\n teardown() {\n if (this.tokenExpirationTimer) {\n clearTimeout(this.tokenExpirationTimer);\n }\n if (this.apiClient?.socket && this.apiClient.socket.isConnected()) {\n this.apiClient.socket.disconnect();\n }\n }\n\n log(message: string, force = false) {\n if (this.logLevel === \"debug\" || force) {\n console.log(`[Knock] ${message}`);\n }\n }\n\n /**\n * Initiates an API client\n */\n private createApiClient() {\n return new ApiClient({\n apiKey: this.apiKey,\n host: this.host,\n userToken: this.userToken,\n });\n }\n\n private async maybeScheduleUserTokenExpiration(\n callbackFn: UserTokenExpiringCallback,\n timeBeforeExpirationInMs: number = 30_000,\n ) {\n if (!this.userToken) return;\n\n const decoded = jwtDecode(this.userToken);\n const expiresAtMs = (decoded.exp ?? 0) * 1000;\n const nowMs = Date.now();\n\n // Expiration is in the future\n if (expiresAtMs && expiresAtMs > nowMs) {\n // Check how long until the token should be regenerated\n // | ----------------- | ----------------------- |\n // ^ now ^ expiration offset ^ expires at\n const msInFuture = expiresAtMs - timeBeforeExpirationInMs - nowMs;\n\n this.tokenExpirationTimer = setTimeout(async () => {\n const newToken = await callbackFn(this.userToken as string, decoded);\n\n // Reauthenticate which will handle reinitializing sockets\n if (typeof newToken === \"string\") {\n this.authenticate(this.userId!, newToken, {\n onUserTokenExpiring: callbackFn,\n timeBeforeExpirationInMs: timeBeforeExpirationInMs,\n });\n }\n }, msInFuture);\n }\n }\n\n /**\n * Returns the user id from the given userIdOrUserWithProperties\n * @param userIdOrUserWithProperties - The user id or user object\n * @returns The user id\n * @throws {Error} If the user object does not contain an `id` property\n */\n private getUserId(userIdOrUserWithProperties: UserIdOrUserWithProperties) {\n if (\n typeof userIdOrUserWithProperties === \"string\" ||\n !userIdOrUserWithProperties\n ) {\n return userIdOrUserWithProperties;\n }\n\n if (userIdOrUserWithProperties?.id) {\n return userIdOrUserWithProperties.id;\n }\n\n throw new Error(\"`user` object must contain an `id` property\");\n }\n}\n\nexport default Knock;\n"],"names":["DEFAULT_HOST","Knock","apiKey","options","__publicField","FeedClient","ObjectClient","Preferences","SlackClient","MsTeamsClient","UserClient","MessageClient","userIdOrUserWithProperties","userToken","reinitializeApi","currentApiClient","userId","id","properties","checkUserToken","_a","message","force","ApiClient","callbackFn","timeBeforeExpirationInMs","decoded","jwtDecode","expiresAtMs","nowMs","msInFuture","newToken"],"mappings":"2lBAmBMA,EAAe,wBAErB,MAAMC,CAAM,CAeV,YACWC,EACTC,EAAwB,GACxB,CAjBKC,EAAA,aACCA,EAAA,iBAA8B,MAC/BA,EAAA,eACAA,EAAA,kBACAA,EAAA,iBACCA,EAAA,4BAA6D,MAC5DA,EAAA,aAAQ,IAAIC,EAAA,QAAW,IAAI,GAC3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t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
- var l = (s, t, e) => S(s, typeof t != "symbol" ? t + "" : t, e);
4
- import { Store as f } from "@tanstack/store";
1
+ var S = Object.defineProperty;
2
+ var c = (t, e, s) => e in t ? S(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
+ var l = (t, e, s) => c(t, typeof e != "symbol" ? e + "" : e, s);
4
+ import { Store as m } from "@tanstack/store";
5
5
  import { NetworkStatus as d } from "../../networkStatus.mjs";
6
- import { deduplicateItems as m, sortItems as p } from "./utils.mjs";
7
- function g(s) {
8
- const t = m(s);
9
- return p(t);
6
+ import { deduplicateItems as f, sortItems as p } from "./utils.mjs";
7
+ function g(t) {
8
+ const e = f(t);
9
+ return p(e);
10
10
  }
11
11
  const I = {
12
12
  shouldSetPage: !0,
@@ -35,70 +35,78 @@ const I = {
35
35
  },
36
36
  setItemAttrs: () => {
37
37
  }
38
- }, h = () => {
39
- const s = new f(i);
40
- return s.setState((t) => ({
41
- ...t,
38
+ }, w = () => {
39
+ const t = new m(i);
40
+ return t.setState((e) => ({
41
+ ...e,
42
42
  // The network status indicates what's happening with the request
43
43
  networkStatus: d.ready,
44
44
  loading: !1,
45
- setNetworkStatus: (e) => s.setState((r) => ({
45
+ setNetworkStatus: (s) => t.setState((r) => ({
46
46
  ...r,
47
- networkStatus: e,
48
- loading: e === d.loading
47
+ networkStatus: s,
48
+ loading: s === d.loading
49
49
  })),
50
- setResult: ({ entries: e, meta: r, page_info: u }, o = I) => s.setState((a) => {
51
- const n = o.shouldAppend ? g(a.items.concat(e)) : e;
50
+ setResult: ({ entries: s, meta: r, page_info: u }, a = I) => t.setState((o) => {
51
+ const n = a.shouldAppend ? g(o.items.concat(s)) : s;
52
52
  return {
53
- ...a,
53
+ ...o,
54
54
  items: n,
55
55
  metadata: r,
56
- pageInfo: o.shouldSetPage ? u : a.pageInfo,
56
+ pageInfo: a.shouldSetPage ? u : o.pageInfo,
57
57
  loading: !1,
58
58
  networkStatus: d.ready
59
59
  };
60
60
  }),
61
- setMetadata: (e) => s.setState((r) => ({ ...r, metadata: e })),
62
- resetStore: (e = i.metadata) => s.setState(() => ({ ...i, metadata: e })),
63
- setItemAttrs: (e, r) => {
64
- const u = e.reduce(
65
- (o, a) => ({ ...o, [a]: r }),
61
+ setMetadata: (s) => t.setState((r) => ({ ...r, metadata: s })),
62
+ resetStore: (s = i.metadata) => t.setState(() => ({
63
+ ...i,
64
+ setResult: t.state.setResult,
65
+ setMetadata: t.state.setMetadata,
66
+ setNetworkStatus: t.state.setNetworkStatus,
67
+ resetStore: t.state.resetStore,
68
+ setItemAttrs: t.state.setItemAttrs,
69
+ metadata: s
70
+ })),
71
+ setItemAttrs: (s, r) => {
72
+ const u = s.reduce(
73
+ (a, o) => ({ ...a, [o]: r }),
66
74
  {}
67
75
  );
68
- return s.setState((o) => {
69
- const a = o.items.map((n) => u[n.id] ? { ...n, ...u[n.id] } : n);
70
- return { ...o, items: a };
76
+ return t.setState((a) => {
77
+ const o = a.items.map((n) => u[n.id] ? { ...n, ...u[n.id] } : n);
78
+ return { ...a, items: o };
71
79
  });
72
80
  }
73
- })), s;
81
+ })), t;
74
82
  };
75
- class w {
76
- constructor(t) {
83
+ class h {
84
+ constructor(e) {
77
85
  l(this, "store");
78
- this.store = t;
86
+ this.store = e;
79
87
  }
80
88
  getState() {
81
89
  return this.store.state;
82
90
  }
83
- setState(t) {
91
+ setState(e) {
84
92
  this.store.setState(
85
- typeof t == "function" ? t : () => t
93
+ typeof e == "function" ? e : () => e
86
94
  );
87
95
  }
88
96
  getInitialState() {
89
97
  return i;
90
98
  }
91
- subscribe(t) {
92
- return this.store.subscribe((e) => t(e.currentVal));
99
+ subscribe(e) {
100
+ return this.store.subscribe((s) => e(s.currentVal));
93
101
  }
94
102
  }
95
- function _() {
96
- const s = h();
97
- return new w(s);
103
+ function N() {
104
+ const t = w();
105
+ return new h(t);
98
106
  }
99
107
  export {
100
- w as FeedStore,
101
- _ as default,
108
+ h as FeedStore,
109
+ N as default,
102
110
  i as initialStoreState
103
111
  };
104
112
  //# sourceMappingURL=store.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.mjs","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem, FeedMetadata, FeedResponse } from \"./interfaces\";\nimport { FeedStoreState, StoreFeedResultOptions } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nexport const initialStoreState: FeedStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n loading: false,\n networkStatus: NetworkStatus.ready,\n setResult: () => {},\n setMetadata: () => {},\n setNetworkStatus: () => {},\n resetStore: () => {},\n setItemAttrs: () => {},\n};\n\n/**\n * Initalize the store with tanstack store so it can be used within our\n * FeedStore class. We do this seperately so that we have the ability to\n * change which store library we use in the future if need be.\n */\nconst initalizeStore = () => {\n const store = new Store(initialStoreState);\n\n store.setState((state) => ({\n ...state,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n store.setState((state) => ({\n ...state,\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info }: FeedResponse,\n options: StoreFeedResultOptions = defaultSetResultOptions,\n ) =>\n store.setState((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n ...state,\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata: FeedMetadata) =>\n store.setState((state) => ({ ...state, metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n store.setState(() => ({ ...initialStoreState, metadata })),\n\n setItemAttrs: (itemIds: Array<string>, attrs: object) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return store.setState((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { ...state, items };\n });\n },\n }));\n\n return store;\n};\n\n/**\n * The FeedStore class is a wrapper for our store solution that's\n * based on the same shape as zustand. This wrapping class allows\n * us to maintain backwards compatibility with the zustand model\n * while still allowing us to utilize tanstack store for the\n * underlying store solution.\n */\nexport class FeedStore {\n store: Store<FeedStoreState>;\n\n constructor(store: Store<FeedStoreState>) {\n this.store = store;\n }\n\n getState() {\n return this.store.state;\n }\n\n setState(\n updater: ((state: FeedStoreState) => FeedStoreState) | FeedStoreState,\n ) {\n this.store.setState(\n typeof updater === \"function\" ? updater : () => updater,\n );\n }\n\n getInitialState() {\n return initialStoreState;\n }\n\n subscribe(listener: (state: FeedStoreState) => void) {\n return this.store.subscribe((state) => listener(state.currentVal));\n }\n}\n\nexport default function createStore() {\n const store = initalizeStore();\n return new FeedStore(store);\n}\n"],"names":["processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","NetworkStatus","initalizeStore","store","Store","state","networkStatus","entries","meta","page_info","options","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item","FeedStore","__publicField","updater","listener","createStore"],"mappings":";;;;;;AASA,SAASA,EAAaC,GAAmB;AACjC,QAAAC,IAAUC,EAAiBF,CAAK;AAG/B,SAFQG,EAAUF,CAA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,MACZR,EAAM,SAAS,CAACE,OAAW,EAAE,GAAGA,GAAO,UAAAM,EAAA,EAAW;AAAA,IAEpD,YAAY,CAACA,IAAWX,EAAkB,aACxCG,EAAM,SAAS,OAAO,EAAE,GAAGH,GAAmB,UAAAW,EAAW,EAAA;AAAA,IAE3D,cAAc,CAACC,GAAwBC,MAAkB;AAEvD,YAAMC,IAA2CF,EAAQ;AAAA,QACvD,CAACG,GAAKC,OAAY,EAAE,GAAGD,GAAK,CAACC,CAAM,GAAGH;QACtC,CAAA;AAAA,MACF;AAEO,aAAAV,EAAM,SAAS,CAACE,MAAU;AAC/B,cAAMV,IAAQU,EAAM,MAAM,IAAI,CAACY,MACzBH,EAAeG,EAAK,EAAE,IACjB,EAAE,GAAGA,GAAM,GAAGH,EAAeG,EAAK,EAAE,EAAE,IAGxCA,CACR;AAEM,eAAA,EAAE,GAAGZ,GAAO,OAAAV,EAAM;AAAA,MAAA,CAC1B;AAAA,IAAA;AAAA,EACH,EACA,GAEKQ;AACT;AASO,MAAMe,EAAU;AAAA,EAGrB,YAAYf,GAA8B;AAF1C,IAAAgB,EAAA;AAGE,SAAK,QAAQhB;AAAA,EAAA;AAAA,EAGf,WAAW;AACT,WAAO,KAAK,MAAM;AAAA,EAAA;AAAA,EAGpB,SACEiB,GACA;AACA,SAAK,MAAM;AAAA,MACT,OAAOA,KAAY,aAAaA,IAAU,MAAMA;AAAA,IAClD;AAAA,EAAA;AAAA,EAGF,kBAAkB;AACT,WAAApB;AAAA,EAAA;AAAA,EAGT,UAAUqB,GAA2C;AAC5C,WAAA,KAAK,MAAM,UAAU,CAAChB,MAAUgB,EAAShB,EAAM,UAAU,CAAC;AAAA,EAAA;AAErE;AAEA,SAAwBiB,IAAc;AACpC,QAAMnB,IAAQD,EAAe;AACtB,SAAA,IAAIgB,EAAUf,CAAK;AAC5B;"}
1
+ {"version":3,"file":"store.mjs","sources":["../../../../src/clients/feed/store.ts"],"sourcesContent":["import { GenericData } from \"@knocklabs/types\";\nimport { Store } from \"@tanstack/store\";\n\nimport { NetworkStatus } from \"../../networkStatus\";\n\nimport { FeedItem, FeedMetadata, FeedResponse } from \"./interfaces\";\nimport { FeedStoreState, StoreFeedResultOptions } from \"./types\";\nimport { deduplicateItems, sortItems } from \"./utils\";\n\nfunction processItems(items: FeedItem[]) {\n const deduped = deduplicateItems(items);\n const sorted = sortItems(deduped);\n\n return sorted;\n}\n\nconst defaultSetResultOptions = {\n shouldSetPage: true,\n shouldAppend: false,\n};\n\nexport const initialStoreState: FeedStoreState = {\n items: [],\n metadata: {\n total_count: 0,\n unread_count: 0,\n unseen_count: 0,\n },\n pageInfo: {\n before: null,\n after: null,\n page_size: 50,\n },\n loading: false,\n networkStatus: NetworkStatus.ready,\n setResult: () => {},\n setMetadata: () => {},\n setNetworkStatus: () => {},\n resetStore: () => {},\n setItemAttrs: () => {},\n};\n\n/**\n * Initalize the store with tanstack store so it can be used within our\n * FeedStore class. We do this seperately so that we have the ability to\n * change which store library we use in the future if need be.\n */\nconst initalizeStore = () => {\n const store = new Store(initialStoreState);\n\n store.setState((state) => ({\n ...state,\n // The network status indicates what's happening with the request\n networkStatus: NetworkStatus.ready,\n loading: false,\n setNetworkStatus: (networkStatus: NetworkStatus) =>\n store.setState((state) => ({\n ...state,\n networkStatus,\n loading: networkStatus === NetworkStatus.loading,\n })),\n\n setResult: (\n { entries, meta, page_info }: FeedResponse,\n options: StoreFeedResultOptions = defaultSetResultOptions,\n ) =>\n store.setState((state) => {\n // We resort the list on set, so concating everything is fine (if a bit suboptimal)\n const items = options.shouldAppend\n ? processItems(state.items.concat(entries as FeedItem<GenericData>[]))\n : entries;\n\n return {\n ...state,\n items,\n metadata: meta,\n pageInfo: options.shouldSetPage ? page_info : state.pageInfo,\n loading: false,\n networkStatus: NetworkStatus.ready,\n };\n }),\n\n setMetadata: (metadata: FeedMetadata) =>\n store.setState((state) => ({ ...state, metadata })),\n\n resetStore: (metadata = initialStoreState.metadata) =>\n store.setState(() => ({\n ...initialStoreState,\n setResult: store.state.setResult,\n setMetadata: store.state.setMetadata,\n setNetworkStatus: store.state.setNetworkStatus,\n resetStore: store.state.resetStore,\n setItemAttrs: store.state.setItemAttrs,\n metadata,\n })),\n\n setItemAttrs: (itemIds: Array<string>, attrs: object) => {\n // Create a map for the items to the updates to be made\n const itemUpdatesMap: { [id: string]: object } = itemIds.reduce(\n (acc, itemId) => ({ ...acc, [itemId]: attrs }),\n {},\n );\n\n return store.setState((state) => {\n const items = state.items.map((item) => {\n if (itemUpdatesMap[item.id]) {\n return { ...item, ...itemUpdatesMap[item.id] };\n }\n\n return item;\n });\n\n return { ...state, items };\n });\n },\n }));\n\n return store;\n};\n\n/**\n * The FeedStore class is a wrapper for our store solution that's\n * based on the same shape as zustand. This wrapping class allows\n * us to maintain backwards compatibility with the zustand model\n * while still allowing us to utilize tanstack store for the\n * underlying store solution.\n */\nexport class FeedStore {\n store: Store<FeedStoreState>;\n\n constructor(store: Store<FeedStoreState>) {\n this.store = store;\n }\n\n getState() {\n return this.store.state;\n }\n\n setState(\n updater: ((state: FeedStoreState) => FeedStoreState) | FeedStoreState,\n ) {\n this.store.setState(\n typeof updater === \"function\" ? updater : () => updater,\n );\n }\n\n getInitialState() {\n return initialStoreState;\n }\n\n subscribe(listener: (state: FeedStoreState) => void) {\n return this.store.subscribe((state) => listener(state.currentVal));\n }\n}\n\nexport default function createStore() {\n const store = initalizeStore();\n return new FeedStore(store);\n}\n"],"names":["processItems","items","deduped","deduplicateItems","sortItems","defaultSetResultOptions","initialStoreState","NetworkStatus","initalizeStore","store","Store","state","networkStatus","entries","meta","page_info","options","metadata","itemIds","attrs","itemUpdatesMap","acc","itemId","item","FeedStore","__publicField","updater","listener","createStore"],"mappings":";;;;;;AASA,SAASA,EAAaC,GAAmB;AACjC,QAAAC,IAAUC,EAAiBF,CAAK;AAG/B,SAFQG,EAAUF,CAAO;AAGlC;AAEA,MAAMG,IAA0B;AAAA,EAC9B,eAAe;AAAA,EACf,cAAc;AAChB,G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AAK;AAC5B;"}
@@ -1,17 +1,17 @@
1
- var l = Object.defineProperty;
2
- var c = (n, e, t) => e in n ? l(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
3
- var i = (n, e, t) => c(n, typeof e != "symbol" ? e + "" : e, t);
4
- import { jwtDecode as u } from "jwt-decode";
5
- import p from "./api.mjs";
6
- import f from "./clients/feed/index.mjs";
7
- import k from "./clients/messages/index.mjs";
8
- import m from "./clients/ms-teams/index.mjs";
9
- import d from "./clients/objects/index.mjs";
10
- import T from "./clients/preferences/index.mjs";
11
- import C from "./clients/slack/index.mjs";
12
- import w from "./clients/users/index.mjs";
13
- const g = "https://api.knock.app";
14
- class v {
1
+ var r = Object.defineProperty;
2
+ var u = (s, e, t) => e in s ? r(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
3
+ var i = (s, e, t) => u(s, typeof e != "symbol" ? e + "" : e, t);
4
+ import { jwtDecode as f } from "jwt-decode";
5
+ import k from "./api.mjs";
6
+ import m from "./clients/feed/index.mjs";
7
+ import p from "./clients/messages/index.mjs";
8
+ import d from "./clients/ms-teams/index.mjs";
9
+ import T from "./clients/objects/index.mjs";
10
+ import g from "./clients/preferences/index.mjs";
11
+ import w from "./clients/slack/index.mjs";
12
+ import C from "./clients/users/index.mjs";
13
+ const y = "https://api.knock.app";
14
+ class S {
15
15
  constructor(e, t = {}) {
16
16
  i(this, "host");
17
17
  i(this, "apiClient", null);
@@ -19,14 +19,14 @@ class v {
19
19
  i(this, "userToken");
20
20
  i(this, "logLevel");
21
21
  i(this, "tokenExpirationTimer", null);
22
- i(this, "feeds", new f(this));
23
- i(this, "objects", new d(this));
24
- i(this, "preferences", new T(this));
25
- i(this, "slack", new C(this));
26
- i(this, "msTeams", new m(this));
27
- i(this, "user", new w(this));
28
- i(this, "messages", new k(this));
29
- if (this.apiKey = e, this.host = t.host || g, this.logLevel = t.logLevel, this.log("Initialized Knock instance"), this.apiKey && this.apiKey.startsWith("sk_"))
22
+ i(this, "feeds", new m(this));
23
+ i(this, "objects", new T(this));
24
+ i(this, "preferences", new g(this));
25
+ i(this, "slack", new w(this));
26
+ i(this, "msTeams", new d(this));
27
+ i(this, "user", new C(this));
28
+ i(this, "messages", new p(this));
29
+ if (this.apiKey = e, this.host = t.host || y, this.logLevel = t.logLevel, this.log("Initialized Knock instance"), this.apiKey && this.apiKey.startsWith("sk_"))
30
30
  throw new Error(
31
31
  "[Knock] You are using your secret API key on the client. Please use the public key."
32
32
  );
@@ -34,16 +34,16 @@ class v {
34
34
  client() {
35
35
  return this.apiClient || (this.apiClient = this.createApiClient()), this.apiClient;
36
36
  }
37
- /*
38
- Authenticates the current user. In non-sandbox environments
39
- the userToken must be specified.
40
- */
41
- authenticate(e, t, s) {
42
- let r = !1;
43
- this.apiClient && (this.userId !== e || this.userToken !== t) && (this.log("userId or userToken changed; reinitializing connections"), this.feeds.teardownInstances(), this.teardown(), r = !0), this.userId = e, this.userToken = t, this.log(`Authenticated with userId ${e}`), this.userToken && (s == null ? void 0 : s.onUserTokenExpiring) instanceof Function && this.maybeScheduleUserTokenExpiration(
44
- s.onUserTokenExpiring,
45
- s.timeBeforeExpirationInMs
46
- ), r && (this.apiClient = this.createApiClient(), this.feeds.reinitializeInstances(), this.log("Reinitialized real-time connections"));
37
+ authenticate(e, t, n) {
38
+ let o = !1;
39
+ const c = this.apiClient, a = this.getUserId(e);
40
+ if (c && (this.userId !== a || this.userToken !== t) && (this.log("userId or userToken changed; reinitializing connections"), this.feeds.teardownInstances(), this.teardown(), o = !0), this.userId = a, this.userToken = t, this.log(`Authenticated with userId ${a}`), this.userToken && (n == null ? void 0 : n.onUserTokenExpiring) instanceof Function && this.maybeScheduleUserTokenExpiration(
41
+ n.onUserTokenExpiring,
42
+ n.timeBeforeExpirationInMs
43
+ ), o && (this.apiClient = this.createApiClient(), this.feeds.reinitializeInstances(), this.log("Reinitialized real-time connections")), typeof e == "object" && (e != null && e.id)) {
44
+ const { id: h, ...l } = e;
45
+ this.user.identify(l);
46
+ }
47
47
  }
48
48
  failIfNotAuthenticated() {
49
49
  if (!this.isAuthenticated())
@@ -68,7 +68,7 @@ class v {
68
68
  * Initiates an API client
69
69
  */
70
70
  createApiClient() {
71
- return new p({
71
+ return new k({
72
72
  apiKey: this.apiKey,
73
73
  host: this.host,
74
74
  userToken: this.userToken
@@ -76,20 +76,33 @@ class v {
76
76
  }
77
77
  async maybeScheduleUserTokenExpiration(e, t = 3e4) {
78
78
  if (!this.userToken) return;
79
- const s = u(this.userToken), r = (s.exp ?? 0) * 1e3, o = Date.now();
80
- if (r && r > o) {
81
- const h = r - t - o;
79
+ const n = f(this.userToken), o = (n.exp ?? 0) * 1e3, c = Date.now();
80
+ if (o && o > c) {
81
+ const a = o - t - c;
82
82
  this.tokenExpirationTimer = setTimeout(async () => {
83
- const a = await e(this.userToken, s);
84
- typeof a == "string" && this.authenticate(this.userId, a, {
83
+ const h = await e(this.userToken, n);
84
+ typeof h == "string" && this.authenticate(this.userId, h, {
85
85
  onUserTokenExpiring: e,
86
86
  timeBeforeExpirationInMs: t
87
87
  });
88
- }, h);
88
+ }, a);
89
89
  }
90
90
  }
91
+ /**
92
+ * Returns the user id from the given userIdOrUserWithProperties
93
+ * @param userIdOrUserWithProperties - The user id or user object
94
+ * @returns The user id
95
+ * @throws {Error} If the user object does not contain an `id` property
96
+ */
97
+ getUserId(e) {
98
+ if (typeof e == "string" || !e)
99
+ return e;
100
+ if (e != null && e.id)
101
+ return e.id;
102
+ throw new Error("`user` object must contain an `id` property");
103
+ }
91
104
  }
92
105
  export {
93
- v as default
106
+ S as default
94
107
  };
95
108
  //# sourceMappingURL=knock.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"knock.mjs","sources":["../../src/knock.ts"],"sourcesContent":["import { jwtDecode } from \"jwt-decode\";\n\nimport ApiClient from \"./api\";\nimport FeedClient from \"./clients/feed\";\nimport MessageClient from \"./clients/messages\";\nimport MsTeamsClient from \"./clients/ms-teams\";\nimport ObjectClient from \"./clients/objects\";\nimport Preferences from \"./clients/preferences\";\nimport SlackClient from \"./clients/slack\";\nimport UserClient from \"./clients/users\";\nimport {\n AuthenticateOptions,\n KnockOptions,\n LogLevel,\n UserTokenExpiringCallback,\n} from \"./interfaces\";\n\nconst DEFAULT_HOST = \"https://api.knock.app\";\n\nclass Knock {\n public host: string;\n private apiClient: ApiClient | null = null;\n public userId: string | undefined | null;\n public userToken?: string;\n public logLevel?: LogLevel;\n private tokenExpirationTimer: ReturnType<typeof setTimeout> | null = null;\n readonly feeds = new FeedClient(this);\n readonly objects = new ObjectClient(this);\n readonly preferences = new Preferences(this);\n readonly slack = new SlackClient(this);\n readonly msTeams = new MsTeamsClient(this);\n readonly user = new UserClient(this);\n readonly messages = new MessageClient(this);\n\n constructor(\n readonly apiKey: string,\n options: KnockOptions = {},\n ) {\n this.host = options.host || DEFAULT_HOST;\n this.logLevel = options.logLevel;\n\n this.log(\"Initialized Knock instance\");\n\n // Fail loudly if we're using the wrong API key\n if (this.apiKey && this.apiKey.startsWith(\"sk_\")) {\n throw new Error(\n \"[Knock] You are using your secret API key on the client. Please use the public key.\",\n );\n }\n }\n\n client() {\n // Initiate a new API client if we don't have one yet\n if (!this.apiClient) {\n this.apiClient = this.createApiClient();\n }\n\n return this.apiClient;\n }\n\n /*\n Authenticates the current user. In non-sandbox environments\n the userToken must be specified.\n */\n authenticate(\n userId: Knock[\"userId\"],\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ) {\n let reinitializeApi = false;\n const currentApiClient = this.apiClient;\n\n // If we've previously been initialized and the values have now changed, then we\n // need to reinitialize any stateful connections we have\n if (\n currentApiClient &&\n (this.userId !== userId || this.userToken !== userToken)\n ) {\n this.log(\"userId or userToken changed; reinitializing connections\");\n this.feeds.teardownInstances();\n this.teardown();\n reinitializeApi = true;\n }\n\n this.userId = userId;\n this.userToken = userToken;\n\n this.log(`Authenticated with userId ${userId}`);\n\n if (this.userToken && options?.onUserTokenExpiring instanceof Function) {\n this.maybeScheduleUserTokenExpiration(\n options.onUserTokenExpiring,\n options.timeBeforeExpirationInMs,\n );\n }\n\n // If we get the signal to reinitialize the api client, then we want to create a new client\n // and the reinitialize any existing feed real-time connections we have so everything continues\n // to work with the new credentials we've been given\n if (reinitializeApi) {\n this.apiClient = this.createApiClient();\n this.feeds.reinitializeInstances();\n this.log(\"Reinitialized real-time connections\");\n }\n\n return;\n }\n\n failIfNotAuthenticated() {\n if (!this.isAuthenticated()) {\n throw new Error(\"Not authenticated. Please call `authenticate` first.\");\n }\n }\n\n /*\n Returns whether or this Knock instance is authenticated. Passing `true` will check the presence\n of the userToken as well.\n */\n isAuthenticated(checkUserToken = false) {\n return checkUserToken ? !!(this.userId && this.userToken) : !!this.userId;\n }\n\n // Used to teardown any connected instances\n teardown() {\n if (this.tokenExpirationTimer) {\n clearTimeout(this.tokenExpirationTimer);\n }\n if (this.apiClient?.socket && this.apiClient.socket.isConnected()) {\n this.apiClient.socket.disconnect();\n }\n }\n\n log(message: string, force = false) {\n if (this.logLevel === \"debug\" || force) {\n console.log(`[Knock] ${message}`);\n }\n }\n\n /**\n * Initiates an API client\n */\n private createApiClient() {\n return new ApiClient({\n apiKey: this.apiKey,\n host: this.host,\n userToken: this.userToken,\n });\n }\n\n private async maybeScheduleUserTokenExpiration(\n callbackFn: UserTokenExpiringCallback,\n timeBeforeExpirationInMs: number = 30_000,\n ) {\n if (!this.userToken) return;\n\n const decoded = jwtDecode(this.userToken);\n const expiresAtMs = (decoded.exp ?? 0) * 1000;\n const nowMs = Date.now();\n\n // Expiration is in the future\n if (expiresAtMs && expiresAtMs > nowMs) {\n // Check how long until the token should be regenerated\n // | ----------------- | ----------------------- |\n // ^ now ^ expiration offset ^ expires at\n const msInFuture = expiresAtMs - timeBeforeExpirationInMs - nowMs;\n\n this.tokenExpirationTimer = setTimeout(async () => {\n const newToken = await callbackFn(this.userToken as string, decoded);\n\n // Reauthenticate which will handle reinitializing sockets\n if (typeof newToken === \"string\") {\n this.authenticate(this.userId!, newToken, {\n onUserTokenExpiring: callbackFn,\n timeBeforeExpirationInMs: timeBeforeExpirationInMs,\n });\n }\n }, msInFuture);\n }\n }\n}\n\nexport default Knock;\n"],"names":["DEFAULT_HOST","Knock","apiKey","options","__publicField","FeedClient","ObjectClient","Preferences","SlackClient","MsTeamsClient","UserClient","MessageClient","userId","userToken","reinitializeApi","checkUserToken","_a","message","force","ApiClient","callbackFn","timeBeforeExpirationInMs","decoded","jwtDecode","expiresAtMs","nowMs","msInFuture","newToken"],"mappings":";;;;;;;;;;;;AAiBA,MAAMA,IAAe;AAErB,MAAMC,EAAM;AAAA,EAeV,YACWC,GACTC,IAAwB,IACxB;AAjBK,IAAAC,EAAA;AACC,IAAAA,EAAA,mBAA8B;AAC/B,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACC,IAAAA,EAAA,8BAA6D;AAC5D,IAAAA,EAAA,eAAQ,IAAIC,EAAW,IAAI;AAC3B,IAAAD,EAAA,iBAAU,IAAIE,EAAa,IAAI;AAC/B,IAAAF,EAAA,qBAAc,IAAIG,EAAY,IAAI;AAClC,IAAAH,EAAA,eAAQ,IAAII,EAAY,IAAI;AAC5B,IAAAJ,EAAA,iBAAU,IAAIK,EAAc,IAAI;AAChC,IAAAL,EAAA,cAAO,IAAIM,EAAW,IAAI;AAC1B,IAAAN,EAAA,kBAAW,IAAIO,EAAc,IAAI;AAYxC,QATS,KAAA,SAAAT,GAGJ,KAAA,OAAOC,EAAQ,QAAQH,GAC5B,KAAK,WAAWG,EAAQ,UAExB,KAAK,IAAI,4BAA4B,GAGjC,KAAK,UAAU,KAAK,OAAO,WAAW,KAAK;AAC7C,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,EACF;AAAA,EAGF,SAAS;AAEH,WAAC,KAAK,cACH,KAAA,YAAY,KAAK,gBAAgB,IAGjC,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOd,aACES,GACAC,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KAAK,UAAW;AAEf,UAAAC,IAAUC,EAAU,KAAK,SAAS,GAClCC,KAAeF,EAAQ,OAAO,KAAK,KACnCG,IAAQ,KAAK,IAAI;AAGnB,QAAAD,KAAeA,IAAcC,GAAO;AAIhC,YAAAC,IAAaF,IAAcH,IAA2BI;AAEvD,WAAA,uBAAuB,WAAW,YAAY;AACjD,cAAME,IAAW,MAAMP,EAAW,KAAK,WAAqBE,CAAO;AAG/D,QAAA,OAAOK,KAAa,YACjB,KAAA,aAAa,KAAK,QAASA,GAAU;AAAA,UACxC,qBAAqBP;AAAA,UACrB,0BAAAC;AAAA,QAAA,CACD;AAAA,SAEFK,CAAU;AAAA,IAAA;AAAA,EACf;AAEJ;"}
1
+ {"version":3,"file":"knock.mjs","sources":["../../src/knock.ts"],"sourcesContent":["import { jwtDecode } from \"jwt-decode\";\n\nimport ApiClient from \"./api\";\nimport FeedClient from \"./clients/feed\";\nimport MessageClient from \"./clients/messages\";\nimport MsTeamsClient from \"./clients/ms-teams\";\nimport ObjectClient from \"./clients/objects\";\nimport Preferences from \"./clients/preferences\";\nimport SlackClient from \"./clients/slack\";\nimport UserClient from \"./clients/users\";\nimport {\n AuthenticateOptions,\n KnockOptions,\n LogLevel,\n UserId,\n UserIdOrUserWithProperties,\n UserTokenExpiringCallback,\n} from \"./interfaces\";\n\nconst DEFAULT_HOST = \"https://api.knock.app\";\n\nclass Knock {\n public host: string;\n private apiClient: ApiClient | null = null;\n public userId: string | undefined | null;\n public userToken?: string;\n public logLevel?: LogLevel;\n private tokenExpirationTimer: ReturnType<typeof setTimeout> | null = null;\n readonly feeds = new FeedClient(this);\n readonly objects = new ObjectClient(this);\n readonly preferences = new Preferences(this);\n readonly slack = new SlackClient(this);\n readonly msTeams = new MsTeamsClient(this);\n readonly user = new UserClient(this);\n readonly messages = new MessageClient(this);\n\n constructor(\n readonly apiKey: string,\n options: KnockOptions = {},\n ) {\n this.host = options.host || DEFAULT_HOST;\n this.logLevel = options.logLevel;\n\n this.log(\"Initialized Knock instance\");\n\n // Fail loudly if we're using the wrong API key\n if (this.apiKey && this.apiKey.startsWith(\"sk_\")) {\n throw new Error(\n \"[Knock] You are using your secret API key on the client. Please use the public key.\",\n );\n }\n }\n\n client() {\n // Initiate a new API client if we don't have one yet\n if (!this.apiClient) {\n this.apiClient = this.createApiClient();\n }\n\n return this.apiClient;\n }\n\n /**\n * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.\n * Please pass a `user` object instead containing an `id` value.\n * example:\n * ```ts\n * knock.authenticate({ id: \"user_123\" });\n * ```\n */\n authenticate(\n userIdOrUserWithProperties: UserId,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ): never;\n authenticate(\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ): void;\n authenticate(\n userIdOrUserWithProperties: UserIdOrUserWithProperties,\n userToken?: Knock[\"userToken\"],\n options?: AuthenticateOptions,\n ) {\n let reinitializeApi = false;\n const currentApiClient = this.apiClient;\n const userId = this.getUserId(userIdOrUserWithProperties);\n\n // If we've previously been initialized and the values have now changed, then we\n // need to reinitialize any stateful connections we have\n if (\n currentApiClient &&\n (this.userId !== userId || this.userToken !== userToken)\n ) {\n this.log(\"userId or userToken changed; reinitializing connections\");\n this.feeds.teardownInstances();\n this.teardown();\n reinitializeApi = true;\n }\n\n this.userId = userId;\n this.userToken = userToken;\n\n this.log(`Authenticated with userId ${userId}`);\n\n if (this.userToken && options?.onUserTokenExpiring instanceof Function) {\n this.maybeScheduleUserTokenExpiration(\n options.onUserTokenExpiring,\n options.timeBeforeExpirationInMs,\n );\n }\n\n // If we get the signal to reinitialize the api client, then we want to create a new client\n // and the reinitialize any existing feed real-time connections we have so everything continues\n // to work with the new credentials we've been given\n if (reinitializeApi) {\n this.apiClient = this.createApiClient();\n this.feeds.reinitializeInstances();\n this.log(\"Reinitialized real-time connections\");\n }\n\n // Inline identify the user if we've been given an object with an id.\n if (\n typeof userIdOrUserWithProperties === \"object\" &&\n userIdOrUserWithProperties?.id\n ) {\n const { id, ...properties } = userIdOrUserWithProperties;\n this.user.identify(properties);\n }\n\n return;\n }\n\n failIfNotAuthenticated() {\n if (!this.isAuthenticated()) {\n throw new Error(\"Not authenticated. Please call `authenticate` first.\");\n }\n }\n\n /*\n Returns whether or this Knock instance is authenticated. Passing `true` will check the presence\n of the userToken as well.\n */\n isAuthenticated(checkUserToken = false) {\n return checkUserToken ? !!(this.userId && this.userToken) : !!this.userId;\n }\n\n // Used to teardown any connected instances\n teardown() {\n if (this.tokenExpirationTimer) {\n clearTimeout(this.tokenExpirationTimer);\n }\n if (this.apiClient?.socket && this.apiClient.socket.isConnected()) {\n this.apiClient.socket.disconnect();\n }\n }\n\n log(message: string, force = false) {\n if (this.logLevel === \"debug\" || force) {\n console.log(`[Knock] ${message}`);\n }\n }\n\n /**\n * Initiates an API client\n */\n private createApiClient() {\n return new ApiClient({\n apiKey: this.apiKey,\n host: this.host,\n userToken: this.userToken,\n });\n }\n\n private async maybeScheduleUserTokenExpiration(\n callbackFn: UserTokenExpiringCallback,\n timeBeforeExpirationInMs: number = 30_000,\n ) {\n if (!this.userToken) return;\n\n const decoded = jwtDecode(this.userToken);\n const expiresAtMs = (decoded.exp ?? 0) * 1000;\n const nowMs = Date.now();\n\n // Expiration is in the future\n if (expiresAtMs && expiresAtMs > nowMs) {\n // Check how long until the token should be regenerated\n // | ----------------- | ----------------------- |\n // ^ now ^ expiration offset ^ expires at\n const msInFuture = expiresAtMs - timeBeforeExpirationInMs - nowMs;\n\n this.tokenExpirationTimer = setTimeout(async () => {\n const newToken = await callbackFn(this.userToken as string, decoded);\n\n // Reauthenticate which will handle reinitializing sockets\n if (typeof newToken === \"string\") {\n this.authenticate(this.userId!, newToken, {\n onUserTokenExpiring: callbackFn,\n timeBeforeExpirationInMs: timeBeforeExpirationInMs,\n });\n }\n }, msInFuture);\n }\n }\n\n /**\n * Returns the user id from the given userIdOrUserWithProperties\n * @param userIdOrUserWithProperties - The user id or user object\n * @returns The user id\n * @throws {Error} If the user object does not contain an `id` property\n */\n private getUserId(userIdOrUserWithProperties: UserIdOrUserWithProperties) {\n if (\n typeof userIdOrUserWithProperties === \"string\" ||\n !userIdOrUserWithProperties\n ) {\n return userIdOrUserWithProperties;\n }\n\n if (userIdOrUserWithProperties?.id) {\n return userIdOrUserWithProperties.id;\n }\n\n throw new Error(\"`user` object must contain an `id` property\");\n }\n}\n\nexport default Knock;\n"],"names":["DEFAULT_HOST","Knock","apiKey","options","__publicField","FeedClient","ObjectClient","Preferences","SlackClient","MsTeamsClient","UserClient","MessageClient","userIdOrUserWithProperties","userToken","reinitializeApi","currentApiClient","userId","id","properties","checkUserToken","_a","message","force","ApiClient","callbackFn","timeBeforeExpirationInMs","decoded","jwtDecode","expiresAtMs","nowMs","msInFuture","newToken"],"mappings":";;;;;;;;;;;;AAmBA,MAAMA,IAAe;AAErB,MAAMC,EAAM;AAAA,EAeV,YACWC,GACTC,IAAwB,IACxB;AAjBK,IAAAC,EAAA;AACC,IAAAA,EAAA,mBAA8B;AAC/B,IAAAA,EAAA;AACA,IAAAA,EAAA;AACA,IAAAA,EAAA;AACC,IAAAA,EAAA,8BAA6D;AAC5D,IAAAA,EAAA,eAAQ,IAAIC,EAAW,IAAI;AAC3B,IAAAD,EAAA,iBAAU,IAAIE,EAAa,IAAI;AAC/B,IAAAF,EAAA,qBAAc,IAAIG,EAAY,IAAI;AAClC,IAAAH,EAAA,eAAQ,IAAII,EAAY,IAAI;AAC5B,IAAAJ,EAAA,iBAAU,IAAIK,EAAc,IAAI;AAChC,IAAAL,EAAA,cAAO,IAAIM,EAAW,IAAI;AAC1B,IAAAN,EAAA,kBAAW,IAAIO,EAAc,IAAI;AAYxC,QATS,KAAA,SAAAT,GAGJ,KAAA,OAAOC,EAAQ,QAAQH,GAC5B,KAAK,WAAWG,EAAQ,UAExB,KAAK,IAAI,4BAA4B,GAGjC,KAAK,UAAU,KAAK,OAAO,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ejE,eAAO,MAAM,iBAAiB,EAAE,cAmB/B,CAAC;AAwEF;;;;;;GAMG;AACH,qBAAa,SAAS;IACpB,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;gBAEjB,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC;IAIxC,QAAQ;IAIR,QAAQ,CACN,OAAO,EAAE,CAAC,CAAC,KAAK,EAAE,cAAc,KAAK,cAAc,CAAC,GAAG,cAAc;IAOvE,eAAe;IAIf,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI;CAGpD;AAED,MAAM,CAAC,OAAO,UAAU,WAAW,cAGlC"}
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../../src/clients/feed/store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAKxC,OAAO,EAAE,cAAc,EAA0B,MAAM,SAAS,CAAC;AAejE,eAAO,MAAM,iBAAiB,EAAE,cAmB/B,CAAC;AAgFF;;;;;;GAMG;AACH,qBAAa,SAAS;IACpB,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;gBAEjB,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC;IAIxC,QAAQ;IAIR,QAAQ,CACN,OAAO,EAAE,CAAC,CAAC,KAAK,EAAE,cAAc,KAAK,cAAc,CAAC,GAAG,cAAc;IAOvE,eAAe;IAIf,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI;CAGpD;AAED,MAAM,CAAC,OAAO,UAAU,WAAW,cAGlC"}
@@ -1,5 +1,6 @@
1
1
  import { GenericData } from '@knocklabs/types';
2
2
  import { JwtPayload } from 'jwt-decode';
3
+ import { default as Knock } from './knock';
3
4
  export type LogLevel = "debug";
4
5
  export interface KnockOptions {
5
6
  host?: string;
@@ -63,4 +64,9 @@ export type RevokeAccessTokenInput = {
63
64
  tenant: string;
64
65
  knockChannelId: string;
65
66
  };
67
+ export type UserId = Knock["userId"];
68
+ export type UserWithProperties = {
69
+ id: UserId;
70
+ } & GenericData;
71
+ export type UserIdOrUserWithProperties = UserId | UserWithProperties;
66
72
  //# sourceMappingURL=interfaces.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../src/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAExC,MAAM,MAAM,QAAQ,GAAG,OAAO,CAAC;AAE/B,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB;AAED,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,WAAW;IAC1C,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,CAAC,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,WAAW,CAAC;AAE3C,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,WAAW;I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ients/preferences';
7
7
  import { default as SlackClient } from './clients/slack';
8
8
  import { default as UserClient } from './clients/users';
9
- import { AuthenticateOptions, KnockOptions, LogLevel } from './interfaces';
9
+ import { AuthenticateOptions, KnockOptions, LogLevel, UserId, UserIdOrUserWithProperties } from './interfaces';
10
10
  declare class Knock {
11
11
  readonly apiKey: string;
12
12
  host: string;
@@ -24,7 +24,16 @@ declare class Knock {
24
24
  readonly messages: MessageClient;
25
25
  constructor(apiKey: string, options?: KnockOptions);
26
26
  client(): ApiClient;
27
- authenticate(userId: Knock["userId"], userToken?: Knock["userToken"], options?: AuthenticateOptions): void;
27
+ /**
28
+ * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.
29
+ * Please pass a `user` object instead containing an `id` value.
30
+ * example:
31
+ * ```ts
32
+ * knock.authenticate({ id: "user_123" });
33
+ * ```
34
+ */
35
+ authenticate(userIdOrUserWithProperties: UserId, userToken?: Knock["userToken"], options?: AuthenticateOptions): never;
36
+ authenticate(userIdOrUserWithProperties: UserIdOrUserWithProperties, userToken?: Knock["userToken"], options?: AuthenticateOptions): void;
28
37
  failIfNotAuthenticated(): void;
29
38
  isAuthenticated(checkUserToken?: boolean): boolean;
30
39
  teardown(): void;
@@ -34,6 +43,13 @@ declare class Knock {
34
43
  */
35
44
  private createApiClient;
36
45
  private maybeScheduleUserTokenExpiration;
46
+ /**
47
+ * Returns the user id from the given userIdOrUserWithProperties
48
+ * @param userIdOrUserWithProperties - The user id or user object
49
+ * @returns The user id
50
+ * @throws {Error} If the user object does not contain an `id` property
51
+ */
52
+ private getUserId;
37
53
  }
38
54
  export default Knock;
39
55
  //# sourceMappingURL=knock.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"knock.d.ts","sourceRoot":"","sources":["../../src/knock.ts"],"names":[],"mappings":"AAEA,OAAO,SAAS,MAAM,OAAO,CAAC;AAC9B,OAAO,UAAU,MAAM,gBAAgB,CAAC;AACxC,OAAO,aAAa,MAAM,oBAAoB,CAAC;AAC/C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K,CAAC,WAAW,CAAC,EAC9B,OAAO,CAAC,EAAE,mBAAmB,GAC5B,IAAI;IAuDP,sBAAsB;IAUtB,eAAe,CAAC,cAAc,UAAQ;IAKtC,QAAQ;IASR,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,UAAQ;IAMlC;;OAEG;IACH,OAAO,CAAC,eAAe;YAQT,gCAAgC;IA+B9C;;;;;OAKG;IACH,OAAO,CAAC,SAAS;CAclB;AAED,eAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knocklabs/client",
3
- "version": "0.14.11",
3
+ "version": "0.15.1",
4
4
  "description": "The clientside library for interacting with Knock",
5
5
  "homepage": "https://github.com/knocklabs/javascript/tree/main/packages/client",
6
6
  "author": "@knocklabs",
@@ -46,12 +46,13 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@babel/cli": "^7.27.2",
49
- "@babel/core": "^7.26.0",
49
+ "@babel/core": "^7.28.0",
50
50
  "@babel/plugin-proposal-class-properties": "^7.16.7",
51
51
  "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
52
- "@babel/plugin-transform-runtime": "^7.25.4",
52
+ "@babel/plugin-transform-runtime": "^7.28.0",
53
53
  "@babel/preset-env": "^7.27.1",
54
54
  "@babel/preset-typescript": "^7.27.0",
55
+ "@codecov/vite-plugin": "^1.9.1",
55
56
  "@types/jsonwebtoken": "^9.0.9",
56
57
  "@typescript-eslint/eslint-plugin": "^8.32.0",
57
58
  "@typescript-eslint/parser": "^8.32.1",
@@ -71,7 +72,7 @@
71
72
  "@knocklabs/types": "^0.1.5",
72
73
  "@tanstack/store": "^0.7.1",
73
74
  "@types/phoenix": "^1.6.6",
74
- "axios": "^1.9.0",
75
+ "axios": "^1.10.0",
75
76
  "axios-retry": "^4.5.0",
76
77
  "eventemitter2": "^6.4.5",
77
78
  "jwt-decode": "^4.0.0",
@@ -84,7 +84,15 @@ const initalizeStore = () => {
84
84
  store.setState((state) => ({ ...state, metadata })),
85
85
 
86
86
  resetStore: (metadata = initialStoreState.metadata) =>
87
- store.setState(() => ({ ...initialStoreState, metadata })),
87
+ store.setState(() => ({
88
+ ...initialStoreState,
89
+ setResult: store.state.setResult,
90
+ setMetadata: store.state.setMetadata,
91
+ setNetworkStatus: store.state.setNetworkStatus,
92
+ resetStore: store.state.resetStore,
93
+ setItemAttrs: store.state.setItemAttrs,
94
+ metadata,
95
+ })),
88
96
 
89
97
  setItemAttrs: (itemIds: Array<string>, attrs: object) => {
90
98
  // Create a map for the items to the updates to be made
package/src/interfaces.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { GenericData } from "@knocklabs/types";
2
2
  import { JwtPayload } from "jwt-decode";
3
3
 
4
+ import Knock from "./knock";
5
+
4
6
  export type LogLevel = "debug";
5
7
 
6
8
  export interface KnockOptions {
@@ -76,3 +78,7 @@ export type RevokeAccessTokenInput = {
76
78
  tenant: string;
77
79
  knockChannelId: string;
78
80
  };
81
+
82
+ export type UserId = Knock["userId"];
83
+ export type UserWithProperties = { id: UserId } & GenericData;
84
+ export type UserIdOrUserWithProperties = UserId | UserWithProperties;
package/src/knock.ts CHANGED
@@ -12,6 +12,8 @@ import {
12
12
  AuthenticateOptions,
13
13
  KnockOptions,
14
14
  LogLevel,
15
+ UserId,
16
+ UserIdOrUserWithProperties,
15
17
  UserTokenExpiringCallback,
16
18
  } from "./interfaces";
17
19
 
@@ -58,17 +60,32 @@ class Knock {
58
60
  return this.apiClient;
59
61
  }
60
62
 
61
- /*
62
- Authenticates the current user. In non-sandbox environments
63
- the userToken must be specified.
64
- */
63
+ /**
64
+ * @deprecated Passing `userId` as a `string` is deprecated and will be removed in a future version.
65
+ * Please pass a `user` object instead containing an `id` value.
66
+ * example:
67
+ * ```ts
68
+ * knock.authenticate({ id: "user_123" });
69
+ * ```
70
+ */
71
+ authenticate(
72
+ userIdOrUserWithProperties: UserId,
73
+ userToken?: Knock["userToken"],
74
+ options?: AuthenticateOptions,
75
+ ): never;
76
+ authenticate(
77
+ userIdOrUserWithProperties: UserIdOrUserWithProperties,
78
+ userToken?: Knock["userToken"],
79
+ options?: AuthenticateOptions,
80
+ ): void;
65
81
  authenticate(
66
- userId: Knock["userId"],
82
+ userIdOrUserWithProperties: UserIdOrUserWithProperties,
67
83
  userToken?: Knock["userToken"],
68
84
  options?: AuthenticateOptions,
69
85
  ) {
70
86
  let reinitializeApi = false;
71
87
  const currentApiClient = this.apiClient;
88
+ const userId = this.getUserId(userIdOrUserWithProperties);
72
89
 
73
90
  // If we've previously been initialized and the values have now changed, then we
74
91
  // need to reinitialize any stateful connections we have
@@ -103,6 +120,15 @@ class Knock {
103
120
  this.log("Reinitialized real-time connections");
104
121
  }
105
122
 
123
+ // Inline identify the user if we've been given an object with an id.
124
+ if (
125
+ typeof userIdOrUserWithProperties === "object" &&
126
+ userIdOrUserWithProperties?.id
127
+ ) {
128
+ const { id, ...properties } = userIdOrUserWithProperties;
129
+ this.user.identify(properties);
130
+ }
131
+
106
132
  return;
107
133
  }
108
134
 
@@ -177,6 +203,27 @@ class Knock {
177
203
  }, msInFuture);
178
204
  }
179
205
  }
206
+
207
+ /**
208
+ * Returns the user id from the given userIdOrUserWithProperties
209
+ * @param userIdOrUserWithProperties - The user id or user object
210
+ * @returns The user id
211
+ * @throws {Error} If the user object does not contain an `id` property
212
+ */
213
+ private getUserId(userIdOrUserWithProperties: UserIdOrUserWithProperties) {
214
+ if (
215
+ typeof userIdOrUserWithProperties === "string" ||
216
+ !userIdOrUserWithProperties
217
+ ) {
218
+ return userIdOrUserWithProperties;
219
+ }
220
+
221
+ if (userIdOrUserWithProperties?.id) {
222
+ return userIdOrUserWithProperties.id;
223
+ }
224
+
225
+ throw new Error("`user` object must contain an `id` property");
226
+ }
180
227
  }
181
228
 
182
229
  export default Knock;