Skip to content

Commit eea8792

Browse files
authored
Merge pull request #160 from eitoball/swr-1.0.x
Update for SWR 1.0.x
2 parents b1bb835 + bfbdddc commit eea8792

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"graphql-request": "^3.4.0",
4040
"micromatch": "^4.0.4",
4141
"pascal-case": "^3.1.2",
42-
"swr": "^0.5.6",
42+
"swr": "^1.0.0",
4343
"tslib": "^2.2.0"
4444
},
4545
"devDependencies": {

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export const plugin: PluginFunction<RawSWRPluginConfig> = (
2525
const allAst = concatAST(documents.map((v) => v.document))
2626

2727
const allFragments: LoadedFragment[] = [
28-
...(allAst.definitions.filter(
29-
(d) => d.kind === Kind.FRAGMENT_DEFINITION
30-
) as FragmentDefinitionNode[]).map((fragmentDef) => ({
28+
...(
29+
allAst.definitions.filter(
30+
(d) => d.kind === Kind.FRAGMENT_DEFINITION
31+
) as FragmentDefinitionNode[]
32+
).map((fragmentDef) => ({
3133
node: fragmentDef,
3234
name: fragmentDef.name.value,
3335
onType: fragmentDef.typeCondition.name.value,

src/visitor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const composeQueryHandler = (
7171
config.autogenKey ? '' : 'id: string, '
7272
}getKey: ${config.typesPrefix}SWRInfiniteKeyLoader${
7373
config.typesSuffix
74-
}<${responseType}, ${variablesType}>, variables${optionalVariables}: ${variablesType}, config?: SWRInfiniteConfigInterface<${responseType}, ClientError>) {
74+
}<${responseType}, ${variablesType}>, variables${optionalVariables}: ${variablesType}, config?: SWRInfiniteConfiguration<${responseType}, ClientError>) {
7575
return useSWRInfinite<${responseType}, ClientError>(
7676
utilsForInfinite.generateGetKey<${responseType}, ${variablesType}>(${
7777
config.autogenKey
@@ -122,24 +122,30 @@ export class SWRVisitor extends ClientSideBaseVisitor<
122122
if (this.config.useTypeImports) {
123123
if (this._enabledInfinite) {
124124
this._additionalImports.push(
125-
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
125+
`import type { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
126+
)
127+
this._additionalImports.push(
128+
`import type { SWRInfiniteConfiguration } from 'swr/infinite';`
126129
)
127130
this._additionalImports.push(
128131
`import useSWR, { useSWRInfinite } from 'swr';`
129132
)
130133
} else {
131134
this._additionalImports.push(
132-
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
135+
`import type { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
133136
)
134137
this._additionalImports.push(`import useSWR from 'swr';`)
135138
}
136139
} else if (this._enabledInfinite) {
137140
this._additionalImports.push(
138-
`import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
141+
`import useSWR, { useSWRInfinite, SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
142+
)
143+
this._additionalImports.push(
144+
`import { SWRInfiniteConfiguration } from 'swr/infinite';`
139145
)
140146
} else {
141147
this._additionalImports.push(
142-
`import useSWR, { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
148+
`import useSWR, { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
143149
)
144150
}
145151
}

tests/outputs/autogenSWRKey.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
2323
useFeed2(variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query, ClientError>) {
2424
return useSWR<Feed2Query, ClientError>(genKey<Feed2QueryVariables>('Feed2', variables), () => sdk.feed2(variables), config);
2525
},
26-
useFeed2Infinite(getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfigInterface<Feed2Query, ClientError>) {
26+
useFeed2Infinite(getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfiguration<Feed2Query, ClientError>) {
2727
return useSWRInfinite<Feed2Query, ClientError>(
2828
utilsForInfinite.generateGetKey<Feed2Query, Feed2QueryVariables>(genKey<Feed2QueryVariables>('Feed2', variables), getKey),
2929
utilsForInfinite.generateFetcher<Feed2Query, Feed2QueryVariables>(sdk.feed2, variables),
@@ -35,7 +35,7 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
3535
useFeed4(variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query, ClientError>) {
3636
return useSWR<Feed4Query, ClientError>(genKey<Feed4QueryVariables>('Feed4', variables), () => sdk.feed4(variables), config);
3737
},
38-
useFeed4Infinite(getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfigInterface<Feed4Query, ClientError>) {
38+
useFeed4Infinite(getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfiguration<Feed4Query, ClientError>) {
3939
return useSWRInfinite<Feed4Query, ClientError>(
4040
utilsForInfinite.generateGetKey<Feed4Query, Feed4QueryVariables>(genKey<Feed4QueryVariables>('Feed4', variables), getKey),
4141
utilsForInfinite.generateFetcher<Feed4Query, Feed4QueryVariables>(sdk.feed4, variables),

tests/outputs/infinite.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
2626
useFeed2(key: SWRKeyInterface, variables: Feed2QueryVariables, config?: SWRConfigInterface<Feed2Query, ClientError>) {
2727
return useSWR<Feed2Query, ClientError>(key, () => sdk.feed2(variables), config);
2828
},
29-
useFeed2Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfigInterface<Feed2Query, ClientError>) {
29+
useFeed2Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed2Query, Feed2QueryVariables>, variables: Feed2QueryVariables, config?: SWRInfiniteConfiguration<Feed2Query, ClientError>) {
3030
return useSWRInfinite<Feed2Query, ClientError>(
3131
utilsForInfinite.generateGetKey<Feed2Query, Feed2QueryVariables>(id, getKey),
3232
utilsForInfinite.generateFetcher<Feed2Query, Feed2QueryVariables>(sdk.feed2, variables),
@@ -38,7 +38,7 @@ export function getSdkWithHooks(client: GraphQLClient, withWrapper: SdkFunctionW
3838
useFeed4(key: SWRKeyInterface, variables?: Feed4QueryVariables, config?: SWRConfigInterface<Feed4Query, ClientError>) {
3939
return useSWR<Feed4Query, ClientError>(key, () => sdk.feed4(variables), config);
4040
},
41-
useFeed4Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfigInterface<Feed4Query, ClientError>) {
41+
useFeed4Infinite(id: string, getKey: SWRInfiniteKeyLoader<Feed4Query, Feed4QueryVariables>, variables?: Feed4QueryVariables, config?: SWRInfiniteConfiguration<Feed4Query, ClientError>) {
4242
return useSWRInfinite<Feed4Query, ClientError>(
4343
utilsForInfinite.generateGetKey<Feed4Query, Feed4QueryVariables>(id, getKey),
4444
utilsForInfinite.generateFetcher<Feed4Query, Feed4QueryVariables>(sdk.feed4, variables),

tests/swr.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async function test() {
107107
})) as Types.ComplexPluginOutput
108108

109109
expect(content.prepend).toContain(
110-
`import useSWR, { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
110+
`import useSWR, { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
111111
)
112112

113113
await validate(content, config, docs, schema, usage)
@@ -124,7 +124,7 @@ async function test() {
124124

125125
expect(content.prepend).toContain(`import useSWR from 'swr';`)
126126
expect(content.prepend).toContain(
127-
`import type { ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface } from 'swr';`
127+
`import type { SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
128128
)
129129

130130
await validate(content, config, docs, schema, usage)
@@ -234,7 +234,10 @@ async function test() {
234234
const usage = basicUsage
235235
const output = await validate(content, config, docs, schema, usage)
236236
expect(content.prepend).toContain(
237-
`import useSWR, { useSWRInfinite, ConfigInterface as SWRConfigInterface, keyInterface as SWRKeyInterface, SWRInfiniteConfigInterface } from 'swr';`
237+
`import useSWR, { useSWRInfinite, SWRConfiguration as SWRConfigInterface, Key as SWRKeyInterface } from 'swr';`
238+
)
239+
expect(content.prepend).toContain(
240+
`import { SWRInfiniteConfiguration } from 'swr/infinite';`
238241
)
239242
expect(output).toContain(readOutput('infinite'))
240243
})

0 commit comments

Comments
 (0)