
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
nuxt-api-party
Advanced tools
This module provides composables to fetch data from an API of your choice securely.
You can customize the composable names! Given json-placeholder
set as the module option name
in your Nuxt config, the composables $jsonPlaceholder
and useJsonPlaceholderData
will be available globally.
useFetch
# pnpm
pnpm add -D nuxt-api-party
# npm
npm i -D nuxt-api-party
Composables will initiate a POST request to the Nuxt server route /api/__api_party__
, which then fetches the actual data for a given route from your API and passes the response back to the template/client. This proxy behaviour has the benefit of omitting CORS issues, since data is sent from server to server.
During server-side rendering, calls to the Nuxt server route will directly call the relevant function (emulating the request), saving an additional API call.
ℹ️ Responses are cached and hydrated to the client. Subsequent calls will return cached responses, saving duplicated requests.
Add nuxt-api-party
to your Nuxt config and tell the module options the name of your API:
// `nuxt.config.ts`
export default defineNuxtConfig({
modules: ['nuxt-api-party'],
apiParty: {
// Needed for the names of the composables
name: 'json-placeholder'
}
})
Set the following environment variables in your project's .env
file:
API_PARTY_BASE_URL=https://jsonplaceholder.typicode.com
# Optionally, add a bearer token
# API_PARTY_TOKEN=test
If you were to call your API json-placeholder
, the generated composables are:
$jsonPlaceholder
– Returns the response data, similar to $fetch
useJsonPlaceholderData
– Returns multiple values similar to useFetch
Finally, fetch data from your API in your template:
<script setup lang="ts">
interface Post {
userId: number
id: number
title: string
body: string
}
// `data` will be typed as `Ref<Post | null>`
const { data, pending, refresh, error } = await useJsonPlaceholderData<Post>('posts/1')
</script>
<template>
<div>
<h1>{{ data?.title }}</h1>
<pre>{{ JSON.stringify(data, undefined, 2) }}</pre>
</div>
</template>
interface ModuleOptions {
/**
* API name used for composables
*
* @remarks
* For example, if you set it to `foo`, the composables will be called `$foo` and `useFooData`
*
* @default 'party'
*/
name?: string
/**
* API base URL
*
* @default process.env.API_PARTY_BASE_URL
*/
url?: string
/**
* Optional API token for bearer authentication
*
* @remarks
* You can set a custom header with the `headers` module option instead
*
* @default process.env.API_PARTY_TOKEN
*/
token?: string
/**
* Custom headers sent with every request to the API
*
* @remarks
* Add authorization headers if you want to use a custom authorization method
*
* @default {}
*/
headers?: Record<string, string>
}
Customize your API's composable names with the name
in your Nuxt config module option. Given it is set to json-placeholder
, the composables $jsonPlaceholder
and useJsonPlaceholderData
will be available globally.
ℹ️ The headings of the following sections aren't available as-is. As an example, the module option
name
is set toparty
.
$party
(Respectively Camel-Cased API Name)Returns the API response data.
Types
function $party<T = any>(
path: string,
opts: ApiFetchOptions = {},
): Promise<T>
type ApiFetchOptions = Pick<
FetchOptions,
'onRequest' | 'onRequestError' | 'onResponse' | 'onResponseError' | 'headers'
>
Example
<script setup lang="ts">
const data = await $party(
'posts/1',
{
async onRequest({ request }) {
console.log(request)
},
async onResponse({ response }) {
console.log(response)
},
async onRequestError({ error }) {
console.log(error)
},
async onResponseError({ error }) {
console.log(error)
},
}
)
</script>
<template>
<div>
<h1>{{ data?.title }}</h1>
</div>
</template>
usePartyData
(Respectively Pascal-Cased API Name)Return values:
By default, Nuxt waits until a refresh
is finished before it can be executed again. Passing true
as parameter skips that wait.
Types
export function usePartyData<T = any>(
path: MaybeComputedRef<string>,
opts: UseApiDataOptions<T> = {},
): AsyncData<T, FetchError | null | true>
type UseApiDataOptions<T> = Pick<
UseFetchOptions<T>,
// Pick from `AsyncDataOptions`
| 'lazy'
| 'default'
| 'watch'
| 'initialCache'
| 'immediate'
// Pick from `FetchOptions`
| 'onRequest'
| 'onRequestError'
| 'onResponse'
| 'onResponseError'
// Pick from `globalThis.RequestInit`
| 'headers'
>
The composable infers most of the useAsyncData
options.
Example
<script setup lang="ts">
const { data, pending, error, refresh } = await usePartyData('posts/1')
</script>
<template>
<div>
<h1>{{ data?.result?.title }}</h1>
<button @click="refresh()">
Refresh
</button>
</div>
</template>
corepack enable
(use npm i -g corepack
for Node.js < 16.10)pnpm install
pnpm run dev:prepare
pnpm run dev
MIT License © 2022 Johann Schopplich
FAQs
Securely connect to any API with a server proxy and generated composables
The npm package nuxt-api-party receives a total of 1,128 weekly downloads. As such, nuxt-api-party popularity was classified as popular.
We found that nuxt-api-party demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.