This commit is contained in:
boufni95 2023-12-09 20:34:09 +01:00
parent 93a1504aa3
commit 46c863721b
12 changed files with 2772 additions and 2605 deletions

View file

@ -26,9 +26,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.GetUserInfo) throw new Error('method: GetUserInfo is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
const response = await methods.GetUserInfo({ ...authContext, ...query, ...params })
const response = await methods.GetUserInfo({rpcName:'GetUserInfo', ctx:authContext })
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -39,9 +37,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.AddProductRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.AddProduct({ ...authContext, ...query, ...params }, request)
const response = await methods.AddProduct({rpcName:'AddProduct', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -49,9 +45,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.NewProductInvoice) throw new Error('method: NewProductInvoice is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
const response = await methods.NewProductInvoice({ ...authContext, ...query, ...params })
const response = await methods.NewProductInvoice({rpcName:'NewProductInvoice', ctx:authContext ,query: req.query||{}})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -62,9 +56,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.GetUserOperationsRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.GetUserOperations({ ...authContext, ...query, ...params }, request)
const response = await methods.GetUserOperations({rpcName:'GetUserOperations', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -75,9 +67,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.NewAddressRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.NewAddress({ ...authContext, ...query, ...params }, request)
const response = await methods.NewAddress({rpcName:'NewAddress', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -88,9 +78,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.PayAddressRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.PayAddress({ ...authContext, ...query, ...params }, request)
const response = await methods.PayAddress({rpcName:'PayAddress', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -101,9 +89,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.NewInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.NewInvoice({ ...authContext, ...query, ...params }, request)
const response = await methods.NewInvoice({rpcName:'NewInvoice', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -114,9 +100,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.DecodeInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.DecodeInvoice({ ...authContext, ...query, ...params }, request)
const response = await methods.DecodeInvoice({rpcName:'DecodeInvoice', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -127,9 +111,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.PayInvoiceRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.PayInvoice({ ...authContext, ...query, ...params }, request)
const response = await methods.PayInvoice({rpcName:'PayInvoice', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -140,9 +122,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
const request = req.body
const error = Types.OpenChannelRequestValidate(request)
if (error !== null) return logErrorAndReturnResponse(error, 'invalid request body', res, logger)
const query = req.query
const params = req.params
const response = await methods.OpenChannel({ ...authContext, ...query, ...params }, request)
const response = await methods.OpenChannel({rpcName:'OpenChannel', ctx:authContext , req: request})
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -150,9 +130,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.GetLnurlWithdrawLink) throw new Error('method: GetLnurlWithdrawLink is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
const response = await methods.GetLnurlWithdrawLink({ ...authContext, ...query, ...params })
const response = await methods.GetLnurlWithdrawLink({rpcName:'GetLnurlWithdrawLink', ctx:authContext })
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -160,9 +138,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.GetLnurlPayLink) throw new Error('method: GetLnurlPayLink is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
const response = await methods.GetLnurlPayLink({ ...authContext, ...query, ...params })
const response = await methods.GetLnurlPayLink({rpcName:'GetLnurlPayLink', ctx:authContext })
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -170,9 +146,7 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.GetLNURLChannelLink) throw new Error('method: GetLNURLChannelLink is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
const response = await methods.GetLNURLChannelLink({ ...authContext, ...query, ...params })
const response = await methods.GetLNURLChannelLink({rpcName:'GetLNURLChannelLink', ctx:authContext })
res({status: 'OK', ...response})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
@ -180,22 +154,128 @@ export default (methods: Types.ServerMethods, opts: NostrOptions) => {
try {
if (!methods.GetLiveUserOperations) throw new Error('method: GetLiveUserOperations is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
methods.GetLiveUserOperations({ ...authContext, ...query, ...params }, (response, err) => {
methods.GetLiveUserOperations({rpcName:'GetLiveUserOperations', ctx:authContext ,cb: (response, err) => {
if (err) { logErrorAndReturnResponse(err, err.message, res, logger)} else { res({status: 'OK', ...response})}
})
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
case 'GetMigrationUpdate':
try {
if (!methods.GetMigrationUpdate) throw new Error('method: GetMigrationUpdate is not implemented')
const authContext = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const query = req.query
const params = req.params
methods.GetMigrationUpdate({ ...authContext, ...query, ...params }, (response, err) => {
methods.GetMigrationUpdate({rpcName:'GetMigrationUpdate', ctx:authContext ,cb: (response, err) => {
if (err) { logErrorAndReturnResponse(err, err.message, res, logger)} else { res({status: 'OK', ...response})}
})
}})
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
case 'BatchUser':
try {
const requests = req.body.requests as Types.UserMethodInputs[]
if (!Array.isArray(requests))throw new Error('invalid body, is not an array')
if (requests.length > 10) throw new Error('too many requests in the batch')
const ctx = await opts.NostrUserAuthGuard(req.appId, req.authIdentifier)
const responses = []
for (let i = 0; i < requests.length; i++) {
const operation = requests[i]
try {
switch(operation.rpcName) {
case 'GetUserInfo':
if (!methods.GetUserInfo) {
responses.push({ status: 'ERROR', reason: 'method not defined: GetUserInfo' })
} else {
const res = await methods.GetUserInfo({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'AddProduct':
if (!methods.AddProduct) {
responses.push({ status: 'ERROR', reason: 'method not defined: AddProduct' })
} else {
const res = await methods.AddProduct({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'NewProductInvoice':
if (!methods.NewProductInvoice) {
responses.push({ status: 'ERROR', reason: 'method not defined: NewProductInvoice' })
} else {
const res = await methods.NewProductInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'GetUserOperations':
if (!methods.GetUserOperations) {
responses.push({ status: 'ERROR', reason: 'method not defined: GetUserOperations' })
} else {
const res = await methods.GetUserOperations({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'NewAddress':
if (!methods.NewAddress) {
responses.push({ status: 'ERROR', reason: 'method not defined: NewAddress' })
} else {
const res = await methods.NewAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'PayAddress':
if (!methods.PayAddress) {
responses.push({ status: 'ERROR', reason: 'method not defined: PayAddress' })
} else {
const res = await methods.PayAddress({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'NewInvoice':
if (!methods.NewInvoice) {
responses.push({ status: 'ERROR', reason: 'method not defined: NewInvoice' })
} else {
const res = await methods.NewInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'DecodeInvoice':
if (!methods.DecodeInvoice) {
responses.push({ status: 'ERROR', reason: 'method not defined: DecodeInvoice' })
} else {
const res = await methods.DecodeInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'PayInvoice':
if (!methods.PayInvoice) {
responses.push({ status: 'ERROR', reason: 'method not defined: PayInvoice' })
} else {
const res = await methods.PayInvoice({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'OpenChannel':
if (!methods.OpenChannel) {
responses.push({ status: 'ERROR', reason: 'method not defined: OpenChannel' })
} else {
const res = await methods.OpenChannel({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'GetLnurlWithdrawLink':
if (!methods.GetLnurlWithdrawLink) {
responses.push({ status: 'ERROR', reason: 'method not defined: GetLnurlWithdrawLink' })
} else {
const res = await methods.GetLnurlWithdrawLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'GetLnurlPayLink':
if (!methods.GetLnurlPayLink) {
responses.push({ status: 'ERROR', reason: 'method not defined: GetLnurlPayLink' })
} else {
const res = await methods.GetLnurlPayLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
case 'GetLNURLChannelLink':
if (!methods.GetLNURLChannelLink) {
responses.push({ status: 'ERROR', reason: 'method not defined: GetLNURLChannelLink' })
} else {
const res = await methods.GetLNURLChannelLink({...operation, ctx}); responses.push({ status: 'OK', ...res })
}
break
default:
throw new Error('unkown rpcName')
}
} catch(ex) {const e = ex as any; logger.error(e.message || e); responses.push({ status: 'ERROR', reason: e.message || e })}
}
res({ status: 'OK', responses })
}catch(ex){ const e = ex as any; logErrorAndReturnResponse(e, e.message || e, res, logger); if (opts.throwErrors) throw e }
break
default: logger.error('unknown rpc call name from nostr event:'+req.rpcName)