Skip to content

Commit de6f7f1

Browse files
committed
initial commit
0 parents  commit de6f7f1

16 files changed

+9669
-0
lines changed

.github/workflows/npm_publish.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish npm Package
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions: read-all
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v1
15+
16+
- uses: actions/setup-node@v3
17+
with:
18+
node-version: 18
19+
- run: yarn install --immutable
20+
- run: yarn build
21+
- run: yarn lint
22+
- run: yarn test
23+
24+
publish-npm:
25+
needs: build
26+
runs-on: ubuntu-latest
27+
permissions:
28+
contents: write
29+
issues: write
30+
id-token: write
31+
pull-requests: write
32+
steps:
33+
- uses: actions/checkout@v1
34+
35+
- uses: actions/setup-node@v3
36+
with:
37+
node-version: 18
38+
registry-url: https://registry.npmjs.org/
39+
- run: yarn install --immutable
40+
- run: yarn build
41+
42+
- name: Release
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
46+
run: |
47+
yarn dlx semantic-release

.github/workflows/pull_requests.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Build and run tests
2+
3+
on:
4+
pull_request:
5+
types: [assigned, opened, synchronize, reopened]
6+
7+
permissions: read-all
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v1
15+
16+
- uses: actions/setup-node@v3
17+
with:
18+
node-version: 18
19+
- run: yarn install --immutable
20+
- run: yarn build
21+
- run: yarn lint
22+
- run: yarn test

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
test_output.log
2+
.idea
3+
node_modules
4+
build
5+
coverage
6+
.nyc_output
7+
*.log
8+
.tap_output
9+
__tests__/petstore.d.ts
10+
11+
.yarn/*
12+
!.yarn/patches
13+
!.yarn/plugins
14+
!.yarn/releases
15+
!.yarn/sdks
16+
!.yarn/versions
17+
18+
.node_repl_history
19+
20+
# TypeScript incremental compilation cache
21+
*.tsbuildinfo
22+
.eslintcache
23+
24+
# Added by coconfig
25+
.eslintignore
26+
.npmignore
27+
tsconfig.json
28+
tsconfig.build.json
29+
jest.config.js
30+
.prettierrc.js
31+
.eslintrc.js
32+
.commitlintrc.json

.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

+541
Large diffs are not rendered by default.

.yarn/releases/yarn-3.6.1.cjs

+874
Large diffs are not rendered by default.

.yarnrc.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
nodeLinker: node-modules
2+
3+
plugins:
4+
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
5+
spec: '@yarnpkg/plugin-interactive-tools'
6+
7+
yarnPath: .yarn/releases/yarn-3.6.1.cjs

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 openapi-typescript-infra
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.PHONY: all
2+
3+
all: __tests__/petstore.d.ts
4+
5+
__tests__/petstore.d.ts: __tests__/petstore.json
6+
yarn dlx openapi-typescript ./__tests__/petstore.json -o ./__tests__/petstore.d.ts
7+
@echo "/* eslint-disable */" > lint-swap.txt
8+
@cat __tests__/petstore.d.ts >> lint-swap.txt
9+
@mv lint-swap.txt __tests__/petstore.d.ts
10+
11+
clean:
12+
rm -f __tests__/petstore.d.ts

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# openapi-typescript-enum
2+
3+
This module adds a transformer to openapi-typescript that generates real enums in addition to the typed unions.

SECURITY.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
| Version | Supported |
6+
| ------- | ------------------ |
7+
| 1.x.x | :white_check_mark: |
8+
9+
## Reporting a Vulnerability
10+
11+
File a GitHub issue for any discovered vulnerabilities, or email [email protected] if it needs to be private.

__tests__/index.spec.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache';
2+
3+
import { type JsonPutBody, TypedRESTDataSource } from '../src/index';
4+
5+
import type { paths } from './petstore';
6+
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();
89+
});

0 commit comments

Comments
 (0)