Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/npm/src/NpmClientContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createContext, useContext, useMemo } from 'react';
import { NpmClient } from 'npmjs-api-client';

const NpmClientContext = createContext<NpmClient | null>(null);

export function NpmClientProvider({ children }: { children: React.ReactNode }) {
const client = useMemo(() => new NpmClient(), []);
return <NpmClientContext value={client}>{children}</NpmClientContext>;
}

export function useNpmClient(): NpmClient {
const client = useContext(NpmClientContext);
return client ?? new NpmClient();
}
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmMaintainer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmUser } from 'npmjs-api-client';
import { type NpmUser } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmMaintainerOptions {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmMaintainer(
options: UseNpmMaintainerOptions = {}
): UseQueryResult<NpmUser, Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmUser, Error>({
queryKey: npmQueryKeys.maintainer(username),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmMaintainerPackages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmSearchResult, type MaintainerPackagesParams } from 'npmjs-api-client';
import { type NpmSearchResult, type MaintainerPackagesParams } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmMaintainerPackagesOptions extends MaintainerPackagesParams {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmMaintainerPackages(
options: UseNpmMaintainerPackagesOptions = {}
): UseQueryResult<NpmSearchResult, Error> {
const { enabled = true, ...params } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

const queryParams = Object.keys(params).length > 0 ? params : undefined;

Expand Down
6 changes: 3 additions & 3 deletions packages/npm/src/hooks/useNpmPackage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useMemo } from 'react';
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmPackument } from 'npmjs-api-client';
import { type NpmPackument } from 'npmjs-api-client';
import { useNpmClient } from '../NpmClientContext.js';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';

export interface UseNpmPackageOptions {
Expand All @@ -12,7 +12,7 @@ export function useNpmPackage(
options: UseNpmPackageOptions = {}
): UseQueryResult<NpmPackument, Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmPackument, Error>({
queryKey: npmQueryKeys.package(name),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageDistTags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmDistTags } from 'npmjs-api-client';
import { type NpmDistTags } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageDistTagsOptions {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmPackageDistTags(
options: UseNpmPackageDistTagsOptions = {}
): UseQueryResult<NpmDistTags, Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmDistTags, Error>({
queryKey: npmQueryKeys.packageDistTags(name),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageDownloadRange.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmDownloadRange, type NpmDownloadPeriod } from 'npmjs-api-client';
import { type NpmDownloadRange, type NpmDownloadPeriod } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageDownloadRangeOptions {
period?: NpmDownloadPeriod;
Expand All @@ -13,7 +14,7 @@ export function useNpmPackageDownloadRange(
options: UseNpmPackageDownloadRangeOptions = {}
): UseQueryResult<NpmDownloadRange, Error> {
const { period = 'last-month', enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmDownloadRange, Error>({
queryKey: npmQueryKeys.packageDownloadRange(name, period),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageDownloads.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmDownloadPoint, type NpmDownloadPeriod } from 'npmjs-api-client';
import { type NpmDownloadPoint, type NpmDownloadPeriod } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageDownloadsOptions {
period?: NpmDownloadPeriod;
Expand All @@ -13,7 +14,7 @@ export function useNpmPackageDownloads(
options: UseNpmPackageDownloadsOptions = {}
): UseQueryResult<NpmDownloadPoint, Error> {
const { period = 'last-month', enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmDownloadPoint, Error>({
queryKey: npmQueryKeys.packageDownloads(name, period),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageLatest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmPackageVersion } from 'npmjs-api-client';
import { type NpmPackageVersion } from 'npmjs-api-client';
import { useNpmClient } from '../NpmClientContext.js';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';

export interface UseNpmPackageLatestOptions {
Expand All @@ -12,7 +13,7 @@ export function useNpmPackageLatest(
options: UseNpmPackageLatestOptions = {}
): UseQueryResult<NpmPackageVersion, Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmPackageVersion, Error>({
queryKey: npmQueryKeys.packageVersion(name, 'latest'),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageMaintainers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmPerson } from 'npmjs-api-client';
import { type NpmPerson } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageMaintainersOptions {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmPackageMaintainers(
options: UseNpmPackageMaintainersOptions = {}
): UseQueryResult<NpmPerson[], Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmPerson[], Error>({
queryKey: npmQueryKeys.packageMaintainers(name),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageVersion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmPackageVersion } from 'npmjs-api-client';
import { type NpmPackageVersion } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageVersionOptions {
enabled?: boolean;
Expand All @@ -13,7 +14,7 @@ export function useNpmPackageVersion(
options: UseNpmPackageVersionOptions = {}
): UseQueryResult<NpmPackageVersion, Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmPackageVersion, Error>({
queryKey: npmQueryKeys.packageVersion(name, version),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmPackageVersions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmPackageVersion } from 'npmjs-api-client';
import { type NpmPackageVersion } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmPackageVersionsOptions {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmPackageVersions(
options: UseNpmPackageVersionsOptions = {}
): UseQueryResult<NpmPackageVersion[], Error> {
const { enabled = true } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

return useQuery<NpmPackageVersion[], Error>({
queryKey: npmQueryKeys.packageVersions(name),
Expand Down
7 changes: 4 additions & 3 deletions packages/npm/src/hooks/useNpmSearch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from 'react';

import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { NpmClient, type NpmSearchResult, type NpmSearchParams } from 'npmjs-api-client';
import { type NpmSearchResult, type NpmSearchParams } from 'npmjs-api-client';
import { npmQueryKeys } from '../keys/npmQueryKeys.js';
import { useNpmClient } from '../NpmClientContext.js';

export interface UseNpmSearchOptions extends Omit<NpmSearchParams, 'text'> {
enabled?: boolean;
Expand All @@ -12,7 +13,7 @@ export function useNpmSearch(
options: UseNpmSearchOptions = {}
): UseQueryResult<NpmSearchResult, Error> {
const { enabled = true, ...rest } = options;
const client = useMemo(() => new NpmClient(), []);
const client = useNpmClient();

const params: NpmSearchParams = { text, ...rest };

Expand Down
1 change: 1 addition & 0 deletions packages/npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// - npmjs-api-client (https://www.npmjs.com/package/npmjs-api-client)
// - @tanstack/react-query

export * from './NpmClientContext.js';
export * from './hooks/useNpmPackage.js';
export * from './hooks/useNpmPackageVersion.js';
export * from './hooks/useNpmPackageLatest.js';
Expand Down
Loading