|
1 |
| -import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'; |
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
2 | 3 |
|
3 |
| -import { type JsonPutBody, TypedRESTDataSource } from '../src/index'; |
| 4 | +import { expect, test } from 'vitest'; |
4 | 5 |
|
5 |
| -import type { paths } from './petstore'; |
| 6 | +import { generate } from '../src/index'; |
6 | 7 |
|
7 |
| -interface ApolloContext { |
8 |
| - petId: number; |
9 |
| -} |
10 |
| - |
11 |
| -class PetStoreDataSource extends TypedRESTDataSource<paths, ApolloContext> { |
12 |
| - override baseURL = 'https://petstore3.swagger.io/api/v3'; |
13 |
| - |
14 |
| - protected willSendRequest() {} |
15 |
| - |
16 |
| - callSimpleMethod() { |
17 |
| - return this.openapi.GET( |
18 |
| - '/pet/{petId}', |
19 |
| - { |
20 |
| - params: { |
21 |
| - path: { |
22 |
| - petId: this.context.petId, |
23 |
| - }, |
24 |
| - }, |
25 |
| - }, |
26 |
| - { |
27 |
| - cacheOptions: { |
28 |
| - ttl: 100000, |
29 |
| - }, |
30 |
| - }, |
31 |
| - ); |
32 |
| - } |
33 |
| - |
34 |
| - uploadBadImage() { |
35 |
| - return this.openapi.POST('/pet/{petId}/uploadImage', { |
36 |
| - params: { |
37 |
| - path: { petId: 1 }, |
38 |
| - query: { additionalMetadata: 'test' }, |
39 |
| - }, |
40 |
| - body: 'binarydata', |
41 |
| - }); |
42 |
| - } |
43 |
| -} |
44 |
| - |
45 |
| -test('should create a client', async () => { |
46 |
| - const petStore = new PetStoreDataSource(); |
47 |
| - const cache = new InMemoryLRUCache(); |
48 |
| - expect(petStore).toBeInstanceOf(PetStoreDataSource); |
49 |
| - petStore.initialize({ |
50 |
| - context: { petId: 1 }, |
51 |
| - cache, |
52 |
| - }); |
53 |
| - |
54 |
| - const response = await petStore.callSimpleMethod(); |
55 |
| - expect(response.name).toBe('Pet1'); |
56 |
| - expect(cache.keys()).toHaveLength(1); |
57 |
| - |
58 |
| - const response2 = await petStore.callSimpleMethod(); |
59 |
| - expect(response2.name).toBe('Pet1'); |
60 |
| - expect(cache.keys()).toHaveLength(1); |
61 |
| - |
62 |
| - const reqSpy = jest.spyOn( |
63 |
| - petStore as unknown as { willSendRequest: () => void }, |
64 |
| - 'willSendRequest', |
65 |
| - ); |
66 |
| - |
67 |
| - await petStore.uploadBadImage().catch((error) => { |
68 |
| - expect(error.extensions?.response).toBeTruthy(); |
69 |
| - expect(error.extensions?.response.status).toBe(415); |
70 |
| - }); |
71 |
| - |
72 |
| - expect(reqSpy).toHaveBeenCalledTimes(1); |
73 |
| - expect(reqSpy).toHaveBeenCalledWith( |
74 |
| - expect.objectContaining({ |
75 |
| - method: 'POST', |
76 |
| - path: '/pet/1/uploadImage', |
77 |
| - body: 'binarydata', |
78 |
| - params: new URLSearchParams({ additionalMetadata: 'test' }), |
79 |
| - }), |
80 |
| - ); |
81 |
| - |
82 |
| - // This is essentially a build time test. |
83 |
| - const body: JsonPutBody<paths['/pet']> = { |
84 |
| - id: 1, |
85 |
| - name: 'Kiki', |
86 |
| - photoUrls: [], |
87 |
| - }; |
88 |
| - expect(body).toBeTruthy(); |
| 8 | +test('should generate a matching document', async () => { |
| 9 | + const now = await generate(path.resolve(__dirname, 'petstore.json'), {}); |
| 10 | + expect(now).toMatch(fs.readFileSync(path.resolve(__dirname, 'snapshot.d.ts'), 'utf8')); |
89 | 11 | });
|
0 commit comments