@@ -22,8 +22,14 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
2222} ) : target , mod ) ) ;
2323
2424//#endregion
25+ let path = require ( "path" ) ;
26+ path = __toESM ( path ) ;
2527let better_sqlite3 = require ( "better-sqlite3" ) ;
2628better_sqlite3 = __toESM ( better_sqlite3 ) ;
29+ let url = require ( "url" ) ;
30+ url = __toESM ( url ) ;
31+ let fs = require ( "fs" ) ;
32+ fs = __toESM ( fs ) ;
2733let __h3ravel_shared = require ( "@h3ravel/shared" ) ;
2834__h3ravel_shared = __toESM ( __h3ravel_shared ) ;
2935let axios = require ( "axios" ) ;
@@ -38,11 +44,33 @@ let __ngrok_ngrok = require("@ngrok/ngrok");
3844__ngrok_ngrok = __toESM ( __ngrok_ngrok ) ;
3945
4046//#region src/db.ts
41- const db = new better_sqlite3 . default ( "app.db" ) ;
42- db . pragma ( "journal_mode = WAL" ) ;
43- function init ( table = "json_store" ) {
44- return db . exec ( `
45- CREATE TABLE IF NOT EXISTS ${ table } (
47+ let db ;
48+ const __dirname$1 = ( 0 , path . dirname ) ( ( 0 , url . fileURLToPath ) ( require ( "url" ) . pathToFileURL ( __filename ) . href ) ) ;
49+ const dirPath = path . default . normalize ( path . default . join ( __dirname$1 , ".." , "data" ) ) ;
50+ ( 0 , fs . mkdirSync ) ( dirPath , { recursive : true } ) ;
51+ /**
52+ * Hook to get or set the database instance.
53+ *
54+ * @returns
55+ */
56+ const useDb = ( ) => {
57+ return [ ( ) => db , ( newDb ) => {
58+ db = newDb ;
59+ const [ { journal_mode } ] = db . pragma ( "journal_mode" ) ;
60+ if ( journal_mode !== "wal" ) db . pragma ( "journal_mode = WAL" ) ;
61+ } ] ;
62+ } ;
63+ const [ getDatabase , setDatabase ] = useDb ( ) ;
64+ setDatabase ( new better_sqlite3 . default ( path . default . join ( dirPath , "app.db" ) ) ) ;
65+ /**
66+ * Initialize the database
67+ *
68+ * @param table
69+ * @returns
70+ */
71+ function init ( ) {
72+ return getDatabase ( ) . exec ( `
73+ CREATE TABLE IF NOT EXISTS json_store (
4674 id INTEGER PRIMARY KEY AUTOINCREMENT,
4775 key TEXT UNIQUE,
4876 value TEXT
@@ -56,30 +84,41 @@ function init(table = "json_store") {
5684* @param value
5785* @returns
5886*/
59- function write ( key , value , table = "json_store" ) {
87+ function write ( key , value ) {
88+ const db$1 = getDatabase ( ) ;
6089 if ( typeof value === "boolean" ) value = value ? "1" : "0" ;
6190 if ( value instanceof Object ) value = JSON . stringify ( value ) ;
62- return db . prepare ( `INSERT INTO ${ table } (key, value)
91+ return db$1 . prepare ( `INSERT INTO json_store (key, value)
6392 VALUES (?, ?)
6493 ON CONFLICT(key) DO UPDATE SET value=excluded.value
6594 ` ) . run ( key , value ) . lastInsertRowid ;
6695}
67- function remove ( key , table = "json_store" ) {
68- return db . prepare ( `DELETE FROM ${ table } WHERE key = ?` ) . run ( key ) . lastInsertRowid ;
96+ /**
97+ * Remove a value from the database
98+ *
99+ * @param key
100+ * @param table
101+ * @returns
102+ */
103+ function remove ( key ) {
104+ return getDatabase ( ) . prepare ( "DELETE FROM json_store WHERE key = ?" ) . run ( key ) . lastInsertRowid ;
69105}
70106/**
71107* Read a value from the database
72108*
73109* @param key
74110* @returns
75111*/
76- function read ( key , table = "json_store" ) {
77- const row = db . prepare ( `SELECT * FROM ${ table } WHERE key = ?` ) . get ( key ) ;
78- if ( row ) try {
79- return JSON . parse ( row . value ) ;
80- } catch {
81- return row . value ;
82- }
112+ function read ( key ) {
113+ const db$1 = getDatabase ( ) ;
114+ try {
115+ const row = db$1 . prepare ( "SELECT * FROM json_store WHERE key = ?" ) . get ( key ) ;
116+ if ( row ) try {
117+ return JSON . parse ( row . value ) ;
118+ } catch {
119+ return row . value ;
120+ }
121+ } catch { }
83122 return null ;
84123}
85124
@@ -260,14 +299,14 @@ async function executeSchema(schema, options) {
260299 if ( schema . method == "GET" ) params = options ;
261300 if ( schema . method == "POST" ) data = options ;
262301 const pathVars = [ ...schema . endpoint . matchAll ( / \{ ( [ ^ } ] + ) \} / g) ] . map ( ( match ) => match [ 1 ] ) ;
263- if ( pathVars . length >= 0 ) for ( const path of pathVars ) schema . endpoint = schema . endpoint . replace ( "{" + path + "}" , options [ path ] ) ;
264- const url = new URL ( schema . endpoint , config . apiBaseURL || "https://api.paystack.co" ) ;
302+ if ( pathVars . length >= 0 ) for ( const path$2 of pathVars ) schema . endpoint = schema . endpoint . replace ( "{" + path$2 + "}" , options [ path$2 ] ) ;
303+ const url$1 = new URL ( schema . endpoint , config . apiBaseURL || "https://api.paystack.co" ) ;
265304 params = {
266305 ...params ,
267- ...Object . fromEntries ( url . searchParams . entries ( ) )
306+ ...Object . fromEntries ( url$1 . searchParams . entries ( ) )
268307 } ;
269308 axios_default . request ( {
270- url : url . pathname ,
309+ url : url$1 . pathname ,
271310 method : schema . method ,
272311 params,
273312 data,
@@ -2895,10 +2934,10 @@ async function refreshIntegration() {
28952934* @param domain
28962935* @returns
28972936*/
2898- function setWebhook ( url , token , integrationId , domain = "test" ) {
2937+ function setWebhook ( url$1 , token , integrationId , domain = "test" ) {
28992938 return new Promise ( ( resolve , reject ) => {
29002939 const data = {
2901- [ domain + "_webhook_endpoint" ] : url ,
2940+ [ domain + "_webhook_endpoint" ] : url$1 ,
29022941 integration : integrationId
29032942 } ;
29042943 axios_default . put ( "/integration/webhooks" , data , { headers : {
@@ -2908,7 +2947,7 @@ function setWebhook(url, token, integrationId, domain = "test") {
29082947 const integration = read ( "selected_integration" ) ;
29092948 write ( "selected_integration" , {
29102949 ...integration ,
2911- [ domain + "_webhook_endpoint" ] : url
2950+ [ domain + "_webhook_endpoint" ] : url$1
29122951 } ) ;
29132952 resolve ( resp . data . message ) ;
29142953 } ) . catch ( ( err ) => {
@@ -3209,16 +3248,16 @@ var WebhookCommand = class extends __h3ravel_musket.Command {
32093248 this . error ( "ERROR: Your session has expired. Please run the `login` command to sign in again." ) ;
32103249 return ;
32113250 }
3212- const url = parseURL ( local_route ) ;
3213- if ( ! url . port ) url . port = "8000" ;
3214- if ( ! url . search || url . search == "?" ) url . search = "" ;
3251+ const url$1 = parseURL ( local_route ) ;
3252+ if ( ! url$1 . port ) url$1 . port = "8000" ;
3253+ if ( ! url$1 . search || url$1 . search == "?" ) url$1 . search = "" ;
32153254 try {
32163255 await __ngrok_ngrok . default . kill ( ) ;
32173256 } catch {
32183257 this . debug ( "No existing ngrok process found to kill." ) ;
32193258 }
32203259 const ngrokURL = ( await __ngrok_ngrok . default . forward ( {
3221- addr : url . port ,
3260+ addr : url$1 . port ,
32223261 authtoken : config . ngrokAuthToken || process . env . NGROK_AUTH_TOKEN ,
32233262 domain : process . env . NGROK_DOMAIN
32243263 } ) ) . url ( ) ;
0 commit comments