Skip to content

Commit 70e2138

Browse files
IMB11ProspectorCopilot
authored
feat: base api-client impl (#4694)
* feat: base api-client impl * fix: doc * feat: start work on module stuff * feat: migrate v2/v3 projects into module system * fix: lint & README.md contributing * refactor: remove utils old api client prototype * fix: lint * fix: api url issues * fix: baseurl in error.vue * fix: readme * fix typo in readme * Update apps/frontend/src/providers/api-client.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Calum H. <hendersoncal117@gmail.com> * Update packages/api-client/src/features/verbose-logging.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Calum H. <hendersoncal117@gmail.com> * Update packages/api-client/src/features/retry.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Calum H. <hendersoncal117@gmail.com> --------- Signed-off-by: Calum H. <hendersoncal117@gmail.com> Co-authored-by: Prospector <6166773+Prospector@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 590ba3c commit 70e2138

File tree

45 files changed

+3155
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3155
-123
lines changed

apps/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@modrinth/moderation": "workspace:*",
4444
"@modrinth/ui": "workspace:*",
4545
"@modrinth/utils": "workspace:*",
46+
"@modrinth/api-client": "workspace:*",
4647
"@pinia/nuxt": "^0.5.1",
4748
"@types/three": "^0.172.0",
4849
"@vintl/vintl": "^4.4.1",

apps/frontend/src/app.vue

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,21 @@
77
</template>
88
<script setup lang="ts">
99
import { NotificationPanel, provideNotificationManager } from '@modrinth/ui'
10-
import { provideApi } from '@modrinth/ui/src/providers/api.ts'
11-
import { RestModrinthApi } from '@modrinth/utils'
1210
1311
import ModrinthLoadingIndicator from '~/components/ui/modrinth-loading-indicator.ts'
1412
13+
import { createModrinthClient, provideModrinthClient } from './providers/api-client.ts'
1514
import { FrontendNotificationManager } from './providers/frontend-notifications.ts'
1615
17-
provideNotificationManager(new FrontendNotificationManager())
18-
19-
provideApi(
20-
new RestModrinthApi((url: string, options?: object) => {
21-
const match = url.match(/^\/v(\d+)\/(.+)$/)
16+
const auth = await useAuth()
17+
const config = useRuntimeConfig()
2218
23-
if (match) {
24-
const apiVersion = Number(match[1])
25-
const path = match[2]
19+
provideNotificationManager(new FrontendNotificationManager())
2620
27-
return useBaseFetch(path, {
28-
...options,
29-
apiVersion,
30-
}) as Promise<Response>
31-
} else {
32-
throw new Error('Invalid format')
33-
}
34-
}),
35-
)
21+
const client = createModrinthClient(auth.value, {
22+
apiBaseUrl: config.public.apiBaseUrl.replace('/v2/', '/'),
23+
archonBaseUrl: config.public.pyroBaseUrl.replace('/v2/', '/'),
24+
rateLimitKey: config.rateLimitKey,
25+
})
26+
provideModrinthClient(client)
3627
</script>

apps/frontend/src/error.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,21 @@ import { IntlFormatted } from '@vintl/vintl/components'
5959
import Logo404 from '~/assets/images/404.svg'
6060
6161
import ModrinthLoadingIndicator from './components/ui/modrinth-loading-indicator.ts'
62+
import { createModrinthClient, provideModrinthClient } from './providers/api-client.ts'
6263
import { FrontendNotificationManager } from './providers/frontend-notifications.ts'
6364
65+
const auth = await useAuth()
66+
const config = useRuntimeConfig()
67+
6468
provideNotificationManager(new FrontendNotificationManager())
69+
70+
const client = createModrinthClient(auth.value, {
71+
apiBaseUrl: config.public.apiBaseUrl.replace('/v2/', '/'),
72+
archonBaseUrl: config.public.pyroBaseUrl.replace('/v2/', '/'),
73+
rateLimitKey: config.rateLimitKey,
74+
})
75+
provideModrinthClient(client)
76+
6577
const { formatMessage } = useVIntl()
6678
6779
const props = defineProps({

apps/frontend/src/pages/[type]/[id]/settings/environment.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
UnsavedChangesPopup,
1010
useSavable,
1111
} from '@modrinth/ui'
12-
import { injectApi } from '@modrinth/ui/src/providers/api.ts'
1312
import { defineMessages, useVIntl } from '@vintl/vintl'
1413
14+
import { injectModrinthClient } from '~/providers/api-client.ts'
15+
1516
const { formatMessage } = useVIntl()
1617
1718
const { currentMember, projectV2, projectV3, refreshProject } = injectProjectPageContext()
1819
const { handleError } = injectNotificationManager()
19-
const api = injectApi()
20+
const client = injectModrinthClient()
2021
2122
const saving = ref(false)
2223
@@ -49,8 +50,8 @@ const { saved, current, reset, save } = useSavable(
4950
({ environment, side_types_migration_review_status }) => {
5051
saving.value = true
5152
side_types_migration_review_status = 'reviewed'
52-
api.projects
53-
.editV3(projectV2.value.id, { environment, side_types_migration_review_status })
53+
client.labrinth.projects_v3
54+
.edit(projectV2.value.id, { environment, side_types_migration_review_status })
5455
.then(() => refreshProject().then(reset))
5556
.catch(handleError)
5657
.finally(() => (saving.value = false))

apps/frontend/src/pages/[type]/[id]/settings/general.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
UnsavedChangesPopup,
88
useSavable,
99
} from '@modrinth/ui'
10-
import { injectApi } from '@modrinth/ui/src/providers/api.ts'
1110
import { defineMessages, type MessageDescriptor, useVIntl } from '@vintl/vintl'
1211
12+
import { injectModrinthClient } from '~/providers/api-client.ts'
13+
1314
const { formatMessage } = useVIntl()
1415
1516
const { projectV2: project, refreshProject } = injectProjectPageContext()
1617
const { handleError } = injectNotificationManager()
17-
const api = injectApi()
18+
const client = injectModrinthClient()
1819
1920
const saving = ref(false)
2021
@@ -34,7 +35,7 @@ const { saved, current, reset, save } = useSavable(
3435
3536
if (data) {
3637
saving.value = true
37-
api.projects
38+
client.labrinth.projects_v2
3839
.edit(project.value.id, { title, description: tagline, slug: url })
3940
.then(() => refreshProject().then(reset))
4041
.catch(handleError)

apps/frontend/src/pages/servers/manage/[id].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ const nodeUnavailableDetails = computed(() => [
10441044
{
10451045
label: 'Node',
10461046
value:
1047-
server.moduleErrors?.general?.error.responseData?.hostname ??
1047+
(server.moduleErrors?.general?.error.responseData as any)?.hostname ??
10481048
server.general?.datacenter ??
10491049
'Unknown',
10501050
type: 'inline' as const,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { AbstractFeature, AuthConfig, NuxtClientConfig } from '@modrinth/api-client'
2+
import {
3+
AuthFeature,
4+
CircuitBreakerFeature,
5+
NuxtCircuitBreakerStorage,
6+
NuxtModrinthClient,
7+
VerboseLoggingFeature,
8+
} from '@modrinth/api-client'
9+
import { createContext } from '@modrinth/ui'
10+
11+
export function createModrinthClient(
12+
auth: { token: string | undefined },
13+
config: { apiBaseUrl: string; archonBaseUrl: string; rateLimitKey?: string },
14+
): NuxtModrinthClient {
15+
const optionalFeatures = [
16+
import.meta.dev ? (new VerboseLoggingFeature() as AbstractFeature) : undefined,
17+
].filter(Boolean) as AbstractFeature[]
18+
19+
const clientConfig: NuxtClientConfig = {
20+
labrinthBaseUrl: config.apiBaseUrl,
21+
archonBaseUrl: config.archonBaseUrl,
22+
rateLimitKey: config.rateLimitKey,
23+
features: [
24+
new AuthFeature({
25+
token: async () => auth.token,
26+
} as AuthConfig),
27+
new CircuitBreakerFeature({
28+
storage: new NuxtCircuitBreakerStorage(),
29+
maxFailures: 3,
30+
resetTimeout: 30000,
31+
}),
32+
...optionalFeatures,
33+
],
34+
}
35+
36+
return new NuxtModrinthClient(clientConfig)
37+
}
38+
39+
export const [injectModrinthClient, provideModrinthClient] = createContext<NuxtModrinthClient>(
40+
'root',
41+
'modrinthClient',
42+
)

0 commit comments

Comments
 (0)