Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
Nestjs Aragami
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
Nestjs Aragami
Commits
f059f518
Commit
f059f518
authored
Jun 03, 2023
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add AragamiCacheInterceptor
parent
c6678c22
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
116 additions
and
10 deletions
+116
-10
index.ts
index.ts
+2
-0
package-lock.json
package-lock.json
+10
-9
package.json
package.json
+2
-1
src/aragami-cache.interceptor.ts
src/aragami-cache.interceptor.ts
+74
-0
src/aragami.ts
src/aragami.ts
+3
-0
src/module-builder.ts
src/module-builder.ts
+15
-0
src/utility/sort-params.ts
src/utility/sort-params.ts
+10
-0
No files found.
index.ts
View file @
f059f518
...
...
@@ -28,3 +28,5 @@ export const InjectAragami = () => Inject(ARAGAMI_TOKEN);
exports
:
[
ARAGAMI_TOKEN
],
})
export
class
AragamiModule
extends
ConfigurableModuleClass
{}
export
*
from
'
./src/aragami-cache.interceptor
'
;
package-lock.json
View file @
f059f518
{
"name"
:
"
myproject
"
,
"name"
:
"
nestjs-aragami
"
,
"version"
:
"1.0.0"
,
"lockfileVersion"
:
2
,
"requires"
:
true
,
"packages"
:
{
""
:
{
"name"
:
"
myproject
"
,
"name"
:
"
nestjs-aragami
"
,
"version"
:
"1.0.0"
,
"license"
:
"MIT"
,
"devDependencies"
:
{
...
...
@@ -24,7 +24,8 @@
},
"peerDependencies"
:
{
"@nestjs/common"
:
"^9.1.2"
,
"aragami"
:
"^1.1.2"
"aragami"
:
"^1.1.2"
,
"rxjs"
:
"^7.8.1"
}
},
"node_modules/@ampproject/remapping"
:
{
...
...
@@ -4347,9 +4348,9 @@
}
},
"node_modules/rxjs"
:
{
"version"
:
"7.
5.7
"
,
"resolved"
:
"https://registry.npmjs.org/rxjs/-/rxjs-7.
5.7
.tgz"
,
"integrity"
:
"sha512-
z9MzKh/UcOqB3i20H6rtrlaE/CgjLOvheWK/9ILrbhROGTweAi1BaFsTT9FbwZi5Trr1qNRs+MXkhmR06awzQA
=="
,
"version"
:
"7.
8.1
"
,
"resolved"
:
"https://registry.npmjs.org/rxjs/-/rxjs-7.
8.1
.tgz"
,
"integrity"
:
"sha512-
AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg
=="
,
"peer"
:
true
,
"dependencies"
:
{
"tslib"
:
"^2.1.0"
...
...
@@ -8256,9 +8257,9 @@
}
},
"rxjs"
:
{
"version"
:
"7.
5.7
"
,
"resolved"
:
"https://registry.npmjs.org/rxjs/-/rxjs-7.
5.7
.tgz"
,
"integrity"
:
"sha512-
z9MzKh/UcOqB3i20H6rtrlaE/CgjLOvheWK/9ILrbhROGTweAi1BaFsTT9FbwZi5Trr1qNRs+MXkhmR06awzQA
=="
,
"version"
:
"7.
8.1
"
,
"resolved"
:
"https://registry.npmjs.org/rxjs/-/rxjs-7.
8.1
.tgz"
,
"integrity"
:
"sha512-
AA3TVj+0A2iuIoQkWEK/tqFjBq2j+6PO6Y0zJcvzLAFhEFIO3HL0vls9hWLncZbAAbK0mar7oZ4V079I/qPMxg
=="
,
"peer"
:
true
,
"requires"
:
{
"tslib"
:
"^2.1.0"
...
...
package.json
View file @
f059f518
...
...
@@ -54,6 +54,7 @@
},
"peerDependencies"
:
{
"@nestjs/common"
:
"^9.1.2"
,
"aragami"
:
"^1.1.2"
"aragami"
:
"^1.1.2"
,
"rxjs"
:
"^7.8.1"
}
}
src/aragami-cache.interceptor.ts
0 → 100644
View file @
f059f518
import
{
CallHandler
,
ExecutionContext
,
HttpAdapterHost
,
Inject
,
NestInterceptor
,
Optional
,
}
from
'
@nestjs/common
'
;
import
{
sortParams
}
from
'
./utility/sort-params
'
;
import
{
InjectAragami
}
from
'
../index
'
;
import
{
Aragami
,
CacheKey
}
from
'
aragami
'
;
import
{
reflector
}
from
'
aragami/dist/src/metadata
'
;
import
{
tap
}
from
'
rxjs
'
;
class
RouteCache
{
@
CacheKey
()
key
:
string
;
value
:
any
;
}
export
class
AragamiCacheInterceptor
implements
NestInterceptor
{
@
Optional
()
@
Inject
()
protected
readonly
httpAdapterHost
:
HttpAdapterHost
;
constructor
(@
InjectAragami
()
protected
aragami
:
Aragami
)
{}
protected
allowedMethods
=
[
'
GET
'
];
async
intercept
(
context
:
ExecutionContext
,
next
:
CallHandler
)
{
const
key
=
await
this
.
trackBy
(
context
);
if
(
!
key
)
{
return
next
.
handle
();
}
const
controllerCls
=
context
.
getClass
();
const
controllerMethodName
=
context
.
getHandler
().
name
;
const
ttl
=
reflector
.
get
(
'
AragamiCacheTTL
'
,
controllerCls
,
controllerMethodName
)
||
reflector
.
get
(
'
AragamiCacheTTL
'
,
controllerCls
);
if
(
!
ttl
)
{
return
next
.
handle
();
}
const
cachedValue
=
await
this
.
aragami
.
get
(
RouteCache
,
key
);
if
(
cachedValue
)
{
return
cachedValue
.
value
;
}
return
next
.
handle
()
.
pipe
(
tap
((
value
)
=>
this
.
aragami
.
set
(
RouteCache
,
{
key
,
value
},
{
ttl
})),
);
}
protected
async
trackBy
(
context
:
ExecutionContext
,
):
Promise
<
string
|
undefined
>
{
if
(
!
(
await
this
.
isRequestCacheable
(
context
)))
{
return
;
}
const
httpAdapter
=
this
.
httpAdapterHost
.
httpAdapter
;
const
isHttpApp
=
httpAdapter
&&
!!
httpAdapter
.
getRequestMethod
;
if
(
!
isHttpApp
)
{
return
;
}
const
url
=
new
URL
(
httpAdapter
.
getRequestUrl
(
context
),
'
http://localhost
'
);
return
`
${
url
.
pathname
}
?
${
sortParams
(
url
.
searchParams
)}
`
;
}
protected
async
isRequestCacheable
(
context
:
ExecutionContext
,
):
Promise
<
boolean
>
{
const
req
=
context
.
switchToHttp
().
getRequest
();
return
this
.
allowedMethods
.
includes
(
req
.
method
);
}
}
src/aragami.ts
0 → 100644
View file @
f059f518
import
{
Aragami
}
from
'
aragami
'
;
export
class
AragamiService
extends
Aragami
{}
src/module-builder.ts
0 → 100644
View file @
f059f518
import
{
ConfigurableModuleBuilder
}
from
'
@nestjs/common
'
;
import
{
AragamiOptions
}
from
'
aragami
'
;
export
const
{
ConfigurableModuleClass
,
MODULE_OPTIONS_TOKEN
}
=
new
ConfigurableModuleBuilder
<
AragamiOptions
>
()
.
setExtras
(
{
isGlobal
:
true
,
},
(
definition
,
extras
)
=>
({
...
definition
,
global
:
extras
.
isGlobal
,
}),
)
.
build
();
src/utility/sort-params.ts
0 → 100644
View file @
f059f518
export
function
sortParams
(
params
:
any
)
{
const
param
=
new
URLSearchParams
(
params
);
const
keys
=
Array
.
from
(
param
.
keys
());
keys
.
sort
();
const
sortedParam
=
new
URLSearchParams
();
keys
.
forEach
((
key
)
=>
{
sortedParam
.
append
(
key
,
param
.
get
(
key
));
});
return
sortedParam
.
toString
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment