-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from buggregator/feature/smtp-attachments-api
Refactors SMTP attachments fetching from HTTP endpoint
- Loading branch information
Showing
9 changed files
with
105 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import type { ServerEvent, NormalizedEvent } from '~/src/shared/types'; | ||
import type { SMTP } from "../types"; | ||
import { normalizeSmtpEvent } from "./normalize-smtp-event"; | ||
import { type TUseSmtpApi, useSmtpApi } from "~/src/shared/lib/use-smtp"; | ||
|
||
type TUseSmtp = () => { | ||
normalizeSmtpEvent: (event: ServerEvent<SMTP>) => NormalizedEvent<SMTP> | ||
smtp: TUseSmtpApi | ||
} | ||
|
||
export const useSmtp: TUseSmtp = () => ({ | ||
normalizeSmtpEvent | ||
}) | ||
export const useSmtp: TUseSmtp = () => { | ||
return { | ||
normalizeSmtpEvent, | ||
smtp: useSmtpApi() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './constants'; | ||
export * from './types'; | ||
export * from './use-events-requests'; | ||
export * from './use-smtp-requests'; | ||
export * from './logger'; | ||
export * from './centrifuge'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Attachment, EventId } from "../../types"; | ||
import { type NuxtApp, useNuxtApp } from "#app"; // eslint-disable-line @conarti/feature-sliced/layers-slices | ||
import { REST_API_URL } from "./constants"; | ||
|
||
type TUseSmtpRequests = () => { | ||
getAttachments: (id: EventId) => Promise<Attachment[]> | ||
} | ||
|
||
// TODO: add 403 response handling | ||
|
||
export const useSmtpRequests: TUseSmtpRequests = () => { | ||
|
||
const app: NuxtApp = useNuxtApp() | ||
const { token } = app.$authToken ?? { token: null } | ||
const headers = { "X-Auth-Token": token || '' } | ||
|
||
const getAttachmentsRestUrl = (id: EventId): string => `${REST_API_URL}/api/smtp/${id}/attachments` | ||
|
||
const getAttachments = (id: EventId) => fetch(getAttachmentsRestUrl(id), { headers }) | ||
.then((response) => response.json()) | ||
.then((response) => { | ||
if (response?.data) { | ||
return response.data as Attachment[] | ||
} | ||
|
||
if (response?.code === 403) { | ||
console.error('Forbidden') | ||
return []; | ||
} | ||
|
||
console.error('Fetch Error') | ||
|
||
return []; | ||
}) | ||
.then((attachments: Attachment[]) => attachments) | ||
|
||
return { | ||
getAttachmentsRestUrl, | ||
getAttachments | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./use-smtp-api"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Attachment, EventId } from '../../types'; | ||
import { useSmtpRequests } from "../io"; | ||
|
||
export type TUseSmtpApi = { | ||
getAttachments: (id: EventId) => Promise<Attachment[]> | ||
} | ||
|
||
export const useSmtpApi: TUseSmtpApi = () => { | ||
const { | ||
getAttachments | ||
} = useSmtpRequests() | ||
|
||
return { | ||
getAttachments | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters