Skip to content

Commit 1691b6e

Browse files
chore: wip
1 parent 0193034 commit 1691b6e

File tree

4 files changed

+98
-88
lines changed

4 files changed

+98
-88
lines changed

storage/framework/core/cache/src/drivers/dynamodb.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CacheDriver } from '@stacksjs/types'
2+
import type { GetOptions } from 'bentocache/types'
23
import process from 'node:process'
34
import { BentoCache, bentostore } from 'bentocache'
45
import { dynamoDbDriver } from 'bentocache/drivers/dynamodb'
@@ -50,15 +51,18 @@ export const dynamodb: CacheDriver = {
5051
value,
5152
})
5253
},
53-
async get(key: string): Promise<string | undefined | null> {
54-
const items = await client.get<string>(key)
54+
async get<T>(key: GetOptions<T>): Promise<T> {
55+
const items = await client.get<T>(key)
5556

5657
return items
5758
},
58-
async getOrSet(key: string, value: string): Promise<string | undefined | null> {
59-
const items = await client.getOrSet(key, () => value)
59+
async getOrSet<T>(key: string, value: T): Promise<T> {
60+
const result = await client.getOrSet<T>({
61+
key,
62+
factory: async () => value,
63+
})
6064

61-
return items
65+
return result
6266
},
6367
async del(key: string): Promise<void> {
6468
await client.delete({ key })
Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
1-
// import { SES } from '@aws-sdk/client-ses'
2-
// import { log } from '@stacksjs/logging'
3-
// import type { RenderOptions } from './template'
4-
// import { template } from './template'
5-
// import type { Message, SendEmailParams } from './types'
6-
7-
// export class Email {
8-
// private client: SES
9-
10-
// constructor(private message: Message) {
11-
// this.client = new SES({ region: 'us-east-1' })
12-
// this.message = message
13-
// }
14-
15-
// public async send(options?: RenderOptions): Promise<{ message: string }> {
16-
// log.info('Sending email...')
17-
// const path = this.message.template
18-
19-
// try {
20-
// const templ = await template(path, options)
21-
22-
// const params: SendEmailParams = {
23-
// Source: this.message.from?.address || '',
24-
25-
// Destination: {
26-
// ToAddresses: [this.message.to],
27-
// },
28-
29-
// Message: {
30-
// Body: {
31-
// Html: {
32-
// Charset: 'UTF-8',
33-
// Data: templ.html,
34-
// },
35-
// },
36-
37-
// Subject: {
38-
// Charset: 'UTF-8',
39-
// Data: this.message.subject,
40-
// },
41-
// },
42-
// }
43-
44-
// await this.client.sendEmail(params)
45-
46-
// let returnMsg: { message: string } = { message: 'Email sent' }
47-
48-
// if (this.message.handle) returnMsg = await this.message.handle()
49-
50-
// await this.onSuccess()
51-
52-
// return returnMsg
53-
// } catch (error) {
54-
// return this.onError(error as Error)
55-
// }
56-
// }
57-
58-
// public async onError(error: Error) {
59-
// log.error(error)
60-
61-
// if (!this.message.onError) return
62-
63-
// return await this.message.onError(error)
64-
// }
65-
66-
// // public onSuccess() {
67-
// // try {
68-
// // if (!this.message.onSuccess) return
69-
70-
// // this.message.onSuccess()
71-
// // } catch (error) {
72-
// // return this.onError(error as Error)
73-
// // }
74-
// // }
75-
// }
76-
77-
// export type { Message }
78-
79-
// // export const email = (options: Message) => new Email(options)
80-
// export default Email
1+
import type { RenderOptions } from './template'
2+
import type { Message, SendEmailParams } from './types'
3+
import { SES } from '@aws-sdk/client-ses'
4+
import { log } from '@stacksjs/logging'
5+
import { template } from './template'
6+
7+
export class Email {
8+
private client: SES
9+
10+
constructor(private message: Message) {
11+
this.client = new SES({ region: 'us-east-1' })
12+
this.message = message
13+
}
14+
15+
public async send(options?: RenderOptions): Promise<{ message: string }> {
16+
log.info('Sending email...')
17+
const path = this.message.template
18+
19+
try {
20+
const templ = await template(path, options)
21+
22+
const params: SendEmailParams = {
23+
Source: this.message.from?.address || '',
24+
25+
Destination: {
26+
ToAddresses: [this.message.to],
27+
},
28+
29+
Message: {
30+
Body: {
31+
Html: {
32+
Charset: 'UTF-8',
33+
Data: templ,
34+
},
35+
},
36+
37+
Subject: {
38+
Charset: 'UTF-8',
39+
Data: this.message.subject,
40+
},
41+
},
42+
}
43+
44+
await this.client.sendEmail(params)
45+
46+
let returnMsg: { message: string } = { message: 'Email sent' }
47+
48+
if (this.message.handle)
49+
returnMsg = await this.message.handle()
50+
51+
await this.onSuccess()
52+
53+
return returnMsg
54+
}
55+
catch (error) {
56+
return this.onError(error as Error)
57+
}
58+
}
59+
60+
public async onError(error: Error) {
61+
log.error(error)
62+
63+
if (!this.message.onError)
64+
return
65+
66+
return await this.message.onError(error)
67+
}
68+
69+
public onSuccess() {
70+
try {
71+
if (!this.message.onSuccess)
72+
return
73+
74+
this.message.onSuccess()
75+
}
76+
catch (error) {
77+
return this.onError(error as Error)
78+
}
79+
}
80+
}
81+
82+
export type { Message }
83+
84+
// export const email = (options: Message) => new Email(options)
85+
export default Email

storage/framework/core/types/src/cache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BentoCache } from 'bentocache'
2+
import type { GetOptions } from 'bentocache/types'
23

34
export interface CacheOptions {
45
/**
@@ -108,8 +109,8 @@ export type CacheConfig = Partial<CacheOptions>
108109
export interface CacheDriver {
109110
set: (key: string, value: string, ttl?: number) => Promise<void>
110111
setForever: (key: string, value: string, ttl?: number) => Promise<void>
111-
get: (key: string) => Promise<string | undefined | null>
112-
getOrSet: (key: string, value: string) => Promise<string | undefined | null>
112+
get: <T> (key: GetOptions<T>) => Promise<T>
113+
getOrSet: <T> (key: string, value: T) => Promise<T>
113114
remove: (key: string) => Promise<void>
114115
has: (key: string) => Promise<boolean>
115116
missing: (key: string) => Promise<boolean>

storage/framework/server-auto-imports.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@
164164
"transactionRequest": true,
165165
"userRequest": true
166166
}
167-
}
167+
}

0 commit comments

Comments
 (0)