Skip to content

Commit

Permalink
Merge pull request #2 from groupon/dbushong/feature/main/swagger2
Browse files Browse the repository at this point in the history
Support Swagger 2.x specs
  • Loading branch information
dbushong authored Nov 1, 2021
2 parents dedff23 + b6698ab commit bcc196d
Show file tree
Hide file tree
Showing 17 changed files with 1,897 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/npm-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
Expand All @@ -26,5 +28,6 @@ jobs:
- run: npm run build --if-present
- run: env
- run: npm test
- run: npx nlm verify
env:
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ CLI and Library to assist in converting OpenAPI Specifications into working

## CLI Usage

The basic idea is that this tool reads in an OpenAPI 3.x specification file
and outputs a TypeScript file which contains all of the required type
The basic idea is that this tool reads in an OpenAPI 3.x or 2.x specification
file and outputs a TypeScript file which contains all of the required type
declarations, as well as a definition of a class which extends `Gofer`.

You may then subclass this class to add a valid constructor as well as any
Expand Down Expand Up @@ -100,13 +100,16 @@ The class and usage examples above will basically work exactly the same.
## API

```ts
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { goferFromOpenAPI } from 'gofer-openapi';

const typeScriptSrc = goferFromOpenAPI(
readFileSync('petstore.yml', 'utf8'),
{ className: 'PetStore', format: 'ts' }
);
async function main() {
const typeScriptSrc = await goferFromOpenAPI(
await readFile('petstore.yml', 'utf8'),
{ className: 'PetStore', format: 'ts' }
);
// ...
}
```

See the the type declarations for full usage.
Expand Down
8 changes: 4 additions & 4 deletions fixtures/petstore-base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ export declare class PetStoreBase extends Gofer {
additionalMetadata?: string;
}): Promise<ApiResponse>;
getInventory(): Promise<Record<string, number>>;
placeOrder(order: Order): Promise<Order>;
placeOrder(order?: Order): Promise<Order>;
getOrderById(opts: {
orderId: number;
}): Promise<Order>;
deleteOrder(opts: {
orderId: number;
}): Promise<void>;
createUser(user: User): Promise<void>;
createUsersWithListInput(users: User[]): Promise<User>;
createUser(user?: User): Promise<void>;
createUsersWithListInput(users?: User[]): Promise<User>;
loginUser(opts?: {
username?: string;
password?: string;
Expand All @@ -94,7 +94,7 @@ export declare class PetStoreBase extends Gofer {
}): Promise<User>;
updateUser(opts: {
username: string;
body: User;
body?: User;
}): Promise<void>;
deleteUser(opts: {
username: string;
Expand Down
8 changes: 4 additions & 4 deletions fixtures/petstore-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class PetStoreBase extends Gofer {
}).json();
}

placeOrder(order: Order): Promise<Order> {
placeOrder(order?: Order): Promise<Order> {
return this.post("/store/order", {
endpointName: "placeOrder",
json: order
Expand Down Expand Up @@ -180,14 +180,14 @@ export class PetStoreBase extends Gofer {
}).json();
}

createUser(user: User): Promise<void> {
createUser(user?: User): Promise<void> {
return this.post("/user", {
endpointName: "createUser",
json: user
}).json();
}

createUsersWithListInput(users: User[]): Promise<User> {
createUsersWithListInput(users?: User[]): Promise<User> {
return this.post("/user/createWithList", {
endpointName: "createUsersWithListInput",
json: users
Expand Down Expand Up @@ -226,7 +226,7 @@ export class PetStoreBase extends Gofer {

updateUser(opts: {
username: string,
body: User,
body?: User,
}): Promise<void> {
return this.put("/user/{username}", {
endpointName: "updateUser",
Expand Down
237 changes: 237 additions & 0 deletions fixtures/petstore2-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/* eslint-disable */
import OtherLib from "other-lib";
export type ApiResponse = {
code?: number,
type?: string,
message?: string,
};
export type Category = {
id?: number,
name?: string,
};
export type Pet = {
id?: number,
category?: Category,
name: string,
photoUrls: string[],
tags?: Tag[],
status?: "available" | "pending" | "sold",
};
export type Tag = {
id?: number,
name?: string,
};
export type Order = {
id?: number,
petId?: number,
quantity?: number,
shipDate?: string,
status?: "placed" | "approved" | "delivered",
complete?: boolean,
};
export type User = {
id?: number,
username?: string,
firstName?: string,
lastName?: string,
email?: string,
password?: string,
phone?: string,
userStatus?: number,
};
export default class PetStore2Base extends OtherLib {
uploadFile(opts: {
petId: number
}): Promise<ApiResponse> {
return this.post("/pet/{petId}/uploadImage", {
endpointName: "uploadFile",
pathParams: {
petId: opts.petId
}
}).json();
}

addPet(pet: Pet): Promise<void> {
return this.post("/pet", {
endpointName: "addPet",
json: pet
}).json();
}

updatePet(pet: Pet): Promise<void> {
return this.put("/pet", {
endpointName: "updatePet",
json: pet
}).json();
}

findPetsByStatus(opts: {
status: ("available" | "pending" | "sold")[]
}): Promise<Pet[]> {
return this.get("/pet/findByStatus", {
endpointName: "findPetsByStatus",
qs: {
status: opts.status
}
}).json();
}

findPetsByTags(opts: {
tags: string[]
}): Promise<Pet[]> {
return this.get("/pet/findByTags", {
endpointName: "findPetsByTags",
qs: {
tags: opts.tags
}
}).json();
}

getPetById(opts: {
petId: number
}): Promise<Pet> {
return this.get("/pet/{petId}", {
endpointName: "getPetById",
pathParams: {
petId: opts.petId
}
}).json();
}

updatePetWithForm(opts: {
petId: number
}): Promise<void> {
return this.post("/pet/{petId}", {
endpointName: "updatePetWithForm",
pathParams: {
petId: opts.petId
}
}).json();
}

deletePet(opts: {
apiKey?: string,
petId: number,
}): Promise<void> {
return this.delete("/pet/{petId}", {
endpointName: "deletePet",
pathParams: {
petId: opts.petId
},
headers: {
api_key: opts.apiKey
}
}).json();
}

getInventory(): Promise<Record<string, number>> {
return this.get("/store/inventory", {
endpointName: "getInventory"
}).json();
}

placeOrder(order: Order): Promise<Order> {
return this.post("/store/order", {
endpointName: "placeOrder",
json: order
}).json();
}

getOrderById(opts: {
orderId: number
}): Promise<Order> {
return this.get("/store/order/{orderId}", {
endpointName: "getOrderById",
pathParams: {
orderId: opts.orderId
}
}).json();
}

deleteOrder(opts: {
orderId: number
}): Promise<void> {
return this.delete("/store/order/{orderId}", {
endpointName: "deleteOrder",
pathParams: {
orderId: opts.orderId
}
}).json();
}

createUsersWithListInput(users: User[]): Promise<void> {
return this.post("/user/createWithList", {
endpointName: "createUsersWithListInput",
json: users
}).json();
}

getUserByName(opts: {
username: string
}): Promise<User> {
return this.get("/user/{username}", {
endpointName: "getUserByName",
pathParams: {
username: opts.username
}
}).json();
}

updateUser(opts: {
username: string,
body: User,
}): Promise<void> {
return this.put("/user/{username}", {
endpointName: "updateUser",
pathParams: {
username: opts.username
},
json: opts.body
}).json();
}

deleteUser(opts: {
username: string
}): Promise<void> {
return this.delete("/user/{username}", {
endpointName: "deleteUser",
pathParams: {
username: opts.username
}
}).json();
}

loginUser(opts: {
username: string,
password: string,
}): Promise<string> {
return this.get("/user/login", {
endpointName: "loginUser",
qs: {
username: opts.username,
password: opts.password
}
}).json();
}

logoutUser(): Promise<void> {
return this.get("/user/logout", {
endpointName: "logoutUser"
}).json();
}

createUsersWithArrayInput(users: User[]): Promise<void> {
return this.post("/user/createWithArray", {
endpointName: "createUsersWithArrayInput",
json: users
}).json();
}

createUser(user: User): Promise<void> {
return this.post("/user", {
endpointName: "createUser",
json: user
}).json();
}

}
Loading

0 comments on commit bcc196d

Please sign in to comment.