@knocklabs/client
Advanced tools
Comparing version 0.11.0-rc.0 to 0.11.0-rc-1.0
# Changelog | ||
## 0.11.0-rc.0 | ||
## 0.11.0-rc-1.0 | ||
@@ -12,4 +12,10 @@ ### Minor Changes | ||
- Updated dependencies [f442962] | ||
- @knocklabs/types@0.1.5-rc.0 | ||
- @knocklabs/types@0.1.5-rc-1.0 | ||
## 0.10.14 | ||
### Patch Changes | ||
- 7510909: fix: ensure axios is always imported correctly | ||
## 0.10.13 | ||
@@ -16,0 +22,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";var o=Object.defineProperty;var a=(s,e,t)=>e in s?o(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var r=(s,e,t)=>a(s,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const n=require("axios"),i=require("axios-retry"),u=require("phoenix");class c{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null,this.axiosClient=n.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken}}),typeof window<"u"&&(this.socket=new u.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),i(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:i.exponentialDelay})}async makeRequest(e){try{const t=await this.axiosClient(e);return{statusCode:t.status<300?"ok":"error",body:t.data,error:void 0,status:t.status}}catch(t){return console.error(t),{statusCode:"error",status:500,body:void 0,error:t}}}canRetryRequest(e){return i.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}}exports.default=c; | ||
"use strict";var a=Object.defineProperty;var n=(s,e,t)=>e in s?a(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t;var r=(s,e,t)=>n(s,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("axios"),o=require("axios-retry"),u=require("phoenix");class c{constructor(e){r(this,"host");r(this,"apiKey");r(this,"userToken");r(this,"axiosClient");r(this,"socket");this.host=e.host,this.apiKey=e.apiKey,this.userToken=e.userToken||null;const t=i.default?i.default:i;this.axiosClient=t.create({baseURL:this.host,headers:{Accept:"application/json","Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-Knock-User-Token":this.userToken}}),typeof window<"u"&&(this.socket=new u.Socket(`${this.host.replace("http","ws")}/ws/v1`,{params:{user_token:this.userToken,api_key:this.apiKey}})),o(this.axiosClient,{retries:3,retryCondition:this.canRetryRequest,retryDelay:o.exponentialDelay})}async makeRequest(e){try{const t=await this.axiosClient(e);return{statusCode:t.status<300?"ok":"error",body:t.data,error:void 0,status:t.status}}catch(t){return console.error(t),{statusCode:"error",status:500,body:void 0,error:t}}}canRetryRequest(e){return o.isNetworkError(e)?!0:e.response?e.response.status>=500&&e.response.status<=599||e.response.status===429:!1}}exports.default=c; | ||
//# sourceMappingURL=api.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";var o=Object.defineProperty;var l=(a,e,t)=>e in a?o(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var r=(a,e,t)=>l(a,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("nanoid"),i=require("../../networkStatus.js"),k=require("./socket-manager.js");class y{constructor(e,t,s={}){r(this,"knock");r(this,"queryKey");r(this,"referenceId");r(this,"unsub");this.channelClient=e,this.messageType=t,this.defaultOptions=s,this.defaultOptions={...e.defaultOptions,...s},this.knock=e.knock,this.queryKey=this.buildQueryKey(this.defaultOptions),this.referenceId=g.nanoid(),this.knock.log(`[IAM] Initialized a client for message ${t}`)}async fetch(){const e=this.defaultOptions;this.queryKey=this.buildQueryKey(e);const s=(this.channelClient.store.state.queries[this.queryKey]??{loading:!1,networkStatus:i.NetworkStatus.ready}).networkStatus;if(!(s&&i.isRequestInFlight(s))){this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.loading,loading:!0});try{const n=await this.knock.user.getInAppMessages({channelId:this.channelClient.channelId,messageType:this.messageType,params:e});return this.channelClient.setQueryResponse(this.queryKey,n),{data:n,status:"ok"}}catch(n){return this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.error,loading:!1}),{status:"error",error:n}}}}getQueryInfoSelector(e){var c;const t=e.queries[this.queryKey];return{messages:(((c=t==null?void 0:t.data)==null?void 0:c.messageIds)??[]).reduce((h,d)=>{const u=e.messages[d];return u&&h.push(u),h},[]),networkStatus:(t==null?void 0:t.networkStatus)??i.NetworkStatus.ready,loading:(t==null?void 0:t.loading)??!1}}async markAsSeen(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{seen_at:new Date().toISOString()}),this.makeStatusUpdate(e,"seen")}async markAsUnseen(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{seen_at:null}),this.makeStatusUpdate(e,"unseen")}async markAsRead(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{read_at:new Date().toISOString()}),this.makeStatusUpdate(e,"read")}async markAsUnread(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{read_at:null}),this.makeStatusUpdate(e,"unread")}async markAsInteracted(e,t){const s=new Date().toISOString(),n=this.getItemIds(e);return this.channelClient.setMessageAttrs(n,{read_at:s,interacted_at:s}),this.makeStatusUpdate(e,"interacted",t)}async markAsArchived(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{archived_at:new Date().toISOString()}),this.makeStatusUpdate(e,"archived")}async markAsUnarchived(e){const t=this.getItemIds(e);return this.channelClient.setMessageAttrs(t,{archived_at:null}),this.makeStatusUpdate(e,"unarchived")}async makeStatusUpdate(e,t,s){const n=this.getItemIds(e);return await this.knock.messages.batchUpdateStatuses(n,t,{metadata:s})}buildQueryKey(e){const t=`/v1/users/${this.knock.userId}/in-app-messages/${this.channelClient.channelId}/${this.messageType}`,s=new URLSearchParams(e).toString();return s?`${t}?${s}`:t}subscribe(){this.unsub=this.channelClient.subscribe(this)}unsubscribe(){this.channelClient.unsubscribe(this)}async handleSocketEvent(e){switch(e.event){case k.SocketEventType.MessageCreated:return await this.fetch();default:throw new Error(`Unhandled socket event: ${e.event}`)}}socketChannelTopic(){return`in_app:${this.messageType}:${this.channelClient.channelId}:${this.knock.userId}`}getItemIds(e){return(Array.isArray(e)?e:[e]).map(s=>s.id)}}exports.InAppMessagesClient=y; | ||
"use strict";var o=Object.defineProperty;var l=(a,t,e)=>t in a?o(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var r=(a,t,e)=>l(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const g=require("nanoid"),i=require("../../networkStatus.js"),k=require("./socket-manager.js");class y{constructor(t,e,s={}){r(this,"knock");r(this,"queryKey");r(this,"referenceId");r(this,"unsub");this.channelClient=t,this.messageType=e,this.defaultOptions=s,this.defaultOptions={...t.defaultOptions,...s},this.knock=t.knock,this.queryKey=this.buildQueryKey(this.defaultOptions),this.referenceId=g.nanoid(),this.knock.log(`[IAM] Initialized a client for message ${e}`)}async fetch(){const t={...this.defaultOptions,trigger_data:JSON.stringify(this.defaultOptions.trigger_data)};this.queryKey=this.buildQueryKey(t);const s=(this.channelClient.store.state.queries[this.queryKey]??{loading:!1,networkStatus:i.NetworkStatus.ready}).networkStatus;if(!(s&&i.isRequestInFlight(s))){this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.loading,loading:!0});try{const n=await this.knock.user.getInAppMessages({channelId:this.channelClient.channelId,messageType:this.messageType,params:t});return this.channelClient.setQueryResponse(this.queryKey,n),{data:n,status:"ok"}}catch(n){return this.channelClient.setQueryStatus(this.queryKey,{networkStatus:i.NetworkStatus.error,loading:!1}),{status:"error",error:n}}}}getQueryInfoSelector(t){var c;const e=t.queries[this.queryKey];return{messages:(((c=e==null?void 0:e.data)==null?void 0:c.messageIds)??[]).reduce((h,d)=>{const u=t.messages[d];return u&&h.push(u),h},[]),networkStatus:(e==null?void 0:e.networkStatus)??i.NetworkStatus.ready,loading:(e==null?void 0:e.loading)??!1}}async markAsSeen(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{seen_at:new Date().toISOString()}),this.makeStatusUpdate(t,"seen")}async markAsUnseen(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{seen_at:null}),this.makeStatusUpdate(t,"unseen")}async markAsRead(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{read_at:new Date().toISOString()}),this.makeStatusUpdate(t,"read")}async markAsUnread(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{read_at:null}),this.makeStatusUpdate(t,"unread")}async markAsInteracted(t,e){const s=new Date().toISOString(),n=this.getItemIds(t);return this.channelClient.setMessageAttrs(n,{read_at:s,interacted_at:s}),this.makeStatusUpdate(t,"interacted",e)}async markAsArchived(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{archived_at:new Date().toISOString()}),this.makeStatusUpdate(t,"archived")}async markAsUnarchived(t){const e=this.getItemIds(t);return this.channelClient.setMessageAttrs(e,{archived_at:null}),this.makeStatusUpdate(t,"unarchived")}async makeStatusUpdate(t,e,s){const n=this.getItemIds(t);return await this.knock.messages.batchUpdateStatuses(n,e,{metadata:s})}buildQueryKey(t){const e=`/v1/users/${this.knock.userId}/in-app-messages/${this.channelClient.channelId}/${this.messageType}`,s=new URLSearchParams(t).toString();return s?`${e}?${s}`:e}subscribe(){this.unsub=this.channelClient.subscribe(this)}unsubscribe(){this.channelClient.unsubscribe(this)}async handleSocketEvent(t){switch(t.event){case k.SocketEventType.MessageCreated:return await this.fetch();default:throw new Error(`Unhandled socket event: ${t.event}`)}}socketChannelTopic(){return`in_app:${this.messageType}:${this.channelClient.channelId}:${this.knock.userId}`}getItemIds(t){return(Array.isArray(t)?t:[t]).map(s=>s.id)}}exports.InAppMessagesClient=y; | ||
//# sourceMappingURL=message-client.js.map |
@@ -14,4 +14,6 @@ import { GenericData } from '@knocklabs/types'; | ||
messageType: string; | ||
params: InAppMessagesClientOptions; | ||
params: Omit<InAppMessagesClientOptions, "trigger_data"> & { | ||
trigger_data: string; | ||
}; | ||
} | ||
//# sourceMappingURL=interfaces.d.ts.map |
{ | ||
"name": "@knocklabs/client", | ||
"version": "0.11.0-rc.0", | ||
"version": "0.11.0-rc-1.0", | ||
"description": "The clientside library for interacting with Knock", | ||
@@ -51,3 +51,3 @@ "homepage": "https://github.com/knocklabs/javascript/tree/main/packages/client", | ||
"@babel/cli": "^7.25.7", | ||
"@babel/core": "^7.25.7", | ||
"@babel/core": "^7.25.8", | ||
"@babel/plugin-proposal-class-properties": "^7.16.7", | ||
@@ -67,10 +67,10 @@ "@babel/plugin-proposal-object-rest-spread": "^7.16.7", | ||
"rimraf": "^6.0.1", | ||
"rollup": "^4.22.5", | ||
"typescript": "^5.6.2", | ||
"rollup": "^4.24.2", | ||
"typescript": "^5.6.3", | ||
"vite": "^5.0.0", | ||
"vitest": "^2.0.5" | ||
"vitest": "^2.1.4" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7.25.6", | ||
"@knocklabs/types": "^0.1.5-rc.0", | ||
"@knocklabs/types": "^0.1.5-rc-1.0", | ||
"@tanstack/store": "^0.5.5", | ||
@@ -77,0 +77,0 @@ "@types/phoenix": "^1.5.4", |
@@ -33,4 +33,11 @@ import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from "axios"; | ||
// Create a retryable axios client | ||
this.axiosClient = axios.create({ | ||
// Create a retryable axios client, but account for issues where the axios export is not | ||
// the default in certain bundlers (Webpack). | ||
// | ||
// NOTE: This is a temporary fix that exists because of this issue: | ||
// https://github.com/axios/axios/issues/6591 | ||
const axiosInstance = // @ts-expect-error Fixing the issue described above | ||
(axios.default ? axios.default : axios) as AxiosStatic; | ||
this.axiosClient = axiosInstance.create({ | ||
baseURL: this.host, | ||
@@ -37,0 +44,0 @@ headers: { |
@@ -60,3 +60,7 @@ import { GenericData } from "@knocklabs/types"; | ||
> { | ||
const params = this.defaultOptions; | ||
const params = { | ||
...this.defaultOptions, | ||
// Convert trigger_data to a string, which the API expects | ||
trigger_data: JSON.stringify(this.defaultOptions.trigger_data), | ||
}; | ||
@@ -63,0 +67,0 @@ this.queryKey = this.buildQueryKey(params); |
@@ -76,5 +76,3 @@ import { GenericData, PageInfo, Tenant } from "@knocklabs/types"; | ||
// Optionally scope all notifications that contain this argument as part of their trigger payload | ||
// TODO(KNO-7140): This currently does not work because the API expects this | ||
// to be a json string. | ||
trigger_data?: GenericData; | ||
} |
@@ -17,3 +17,5 @@ import { GenericData } from "@knocklabs/types"; | ||
messageType: string; | ||
params: InAppMessagesClientOptions; | ||
params: Omit<InAppMessagesClientOptions, "trigger_data"> & { | ||
trigger_data: string; | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
454102
4772