Skip to content

Commit b80759b

Browse files
committed
feat(cli): allow for overriding the path and method
1 parent 2d05435 commit b80759b

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ paths:
390390
post:
391391
operationId: createWidget
392392
requestBody:
393-
application/json:
394-
schema: { $ref: '#/components/schemas/CreateWidgetInput' }
393+
required: true
394+
content:
395+
application/json:
396+
schema: { $ref: '#/components/schemas/CreateWidgetInput' }
395397
responses:
396398
201:
397399
description: Widget created without issue
@@ -403,17 +405,14 @@ paths:
403405
```
404406
</details>
405407

406-
### (TODO) Changing the HTTP method and URL
407-
408-
> **Warning**
409-
> This is not yet implemented
408+
### Changing the HTTP method and URL
410409

411410
By default parameters will end up in the default location (`query` for `query` operations and `requestBody` for `mutation` operations). You can use the keywords `method` and `path` to override the default HTTP method and resulting path respectively. You can even specify path parameters, though you will then need to override the parameters mappings.
412411

413412
> **Note**
414413
> The YARPC operations can happily live alongside your standard [`paths` object][oas:paths-object] in the specification. The library will merge the resulting paths objects.
415414

416-
For example, you may want to make a RESTful `modifyWidget` route that modifies a widget owned by a user in a RESTful manner:
415+
For example, you may want to make a RESTful `modifyWidget` route that modifies a widget in a RESTful manner:
417416

418417
```yaml
419418
operations:
@@ -448,11 +447,16 @@ paths:
448447
parameters:
449448
- name: widgetId
450449
in: path
451-
schema: { $ref: '#/components/schemas/ModifyWidgetInput/properties/widgetId' }
450+
schema: { $ref: '#/components/schemas/WidgetID' }
452451
requestBody:
453452
application/json:
454-
// note the `Body` label appended to the end
455-
schema: { $ref: '#/components/schemas/ModifyWidgetInputBody' }
453+
schema:
454+
type: object
455+
required:
456+
- userId
457+
properties:
458+
userId: { $ref: '#/components/schemas/UserID' }
459+
...
456460
```
457461
</details>
458462

packages/cli/src/transformers/document-transformer.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,42 @@ test("DocumentTransformer#transform", async (t) => {
140140
const actual = await transformer.transform();
141141
assert.deepStrictEqual(actual, oasDocument);
142142
});
143+
144+
await t.test("override query method and path", async () => {
145+
const doc = {
146+
...rpcDocument,
147+
operations: {
148+
queries: {
149+
deepHealthCheck: {
150+
method: "post",
151+
path: "/health/deep",
152+
153+
output: {
154+
description: "No content",
155+
statusCode: 204,
156+
},
157+
},
158+
},
159+
},
160+
} as const;
161+
162+
const expected = {
163+
...oasDocument,
164+
paths: {
165+
...oasDocument.paths,
166+
"/health/deep": {
167+
post: {
168+
operationId: "deepHealthCheck",
169+
responses: {
170+
204: { description: "No content" },
171+
},
172+
},
173+
},
174+
},
175+
};
176+
177+
const transformer = new DocumentTransformer(doc);
178+
const actual = await transformer.transform();
179+
assert.deepStrictEqual(actual, expected);
180+
});
143181
});

packages/cli/src/transformers/document-transformer.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,24 @@ export class DocumentTransformer {
5050
async rpcToPaths(rpc: RPCDocument["operations"]): Promise<PathsObject> {
5151
const paths: PathsObject = {};
5252

53-
for (const [operationId, operation] of Object.entries(rpc.queries)) {
54-
paths[`/queries/${operationId}`] = {
55-
get: await this.transformer.transformQueryOperation(
53+
for (const [operationId, operation] of Object.entries(rpc.queries ?? {})) {
54+
const httpMethod = operation.method ?? "get";
55+
56+
paths[operation.path ?? `/queries/${operationId}`] = {
57+
[httpMethod]: await this.transformer.transformQueryOperation(
5658
operationId,
5759
operation
5860
),
5961
};
6062
}
6163

62-
for (const [operationId, operation] of Object.entries(rpc.mutations)) {
63-
paths[`/mutations/${operationId}`] = {
64-
post: await this.transformer.transformMutationOperation(
64+
for (const [operationId, operation] of Object.entries(
65+
rpc.mutations ?? {}
66+
)) {
67+
const httpMethod = operation.method ?? "post";
68+
69+
paths[operation.path ?? `/mutations/${operationId}`] = {
70+
[httpMethod]: await this.transformer.transformMutationOperation(
6571
operationId,
6672
operation
6773
),

0 commit comments

Comments
 (0)