Skip to content

Commit 093421a

Browse files
committed
core: accept method in values + doc updates
1 parent 8df740f commit 093421a

File tree

9 files changed

+92
-56
lines changed

9 files changed

+92
-56
lines changed

packages/core/src/core/pardon.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ export const PardonFetchExecution = pardonExecution({
159159
async match({ context: { url, init, ...context } }) {
160160
const fetchObject = fetchIntoObject(url, init);
161161

162+
if (typeof context.values?.method === "string") {
163+
fetchObject.method ??= context.values?.method;
164+
165+
if (context.values?.method !== fetchObject.method) {
166+
throw new Error(
167+
"specified values method does not match reqeust method",
168+
);
169+
}
170+
}
171+
162172
// pathname undefined in some places is allowed (matches any template),
163173
// but we want to ensure it's set when matching requests.
164174
// but allow undefined origin and pathname for undefined URLs matched/rendered only by values.

packages/core/src/entry/main/cli/options.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ export async function processOptions(
123123
method = args.shift()!;
124124
}
125125

126+
if (values.method !== undefined && typeof values.method !== "string") {
127+
throw new Error("method should be a string");
128+
}
129+
126130
if (!args.length) {
127-
return { values, init: { method: method ?? "GET" } };
131+
return {
132+
values,
133+
init: { method: (method ?? values.method ?? "GET") as string },
134+
};
128135
}
129136

130137
const request = await parseMainArgument(args.shift()!);
@@ -138,7 +145,15 @@ export async function processOptions(
138145
throw new PardonError("http method specified twice");
139146
}
140147

141-
request.method ??= method;
148+
if (typeof values.method !== "string") {
149+
throw new Error("method in values must be a string.");
150+
}
151+
152+
request.method ??= method ?? values.method ?? "GET";
153+
154+
if (values.method && values.method && values.method !== request.method) {
155+
throw new Error("method in input does not match method in request");
156+
}
142157

143158
if (data !== undefined && dataRaw !== undefined) {
144159
throw new Error("both --data and --data-raw should not be specified");
@@ -160,10 +175,6 @@ export async function processOptions(
160175
request!.headers.append(key, value);
161176
}
162177

163-
if (!values.endpoint && !values.method) {
164-
request.method ??= method ?? "GET";
165-
}
166-
167178
return { url: intoURL(request), init: request, values };
168179
}
169180

packages/docs/src/components/Exercises.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const props = Astro.props;
1919

2020
<div
2121
{...props}
22-
class={"relative border border-gray-500 p-3 pt-0" + (props.class ?? "")}
22+
class={"relative border border-gray-500 p-3 pt-0 " + (props.class ?? "")}
2323
>
2424
<div
2525
class="absolute right-[-55px] top-[-12px] bg-red-700 px-12 text-white shadow-sm shadow-black rotate-[30deg] dark:shadow-sm dark:shadow-red-900"

packages/docs/src/components/PardonPlayground.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const { secrets, editor, values, data, response, ...props } = Astro.props;
4242

4343
<style is:global>
4444
.pp-container {
45-
@apply mt-4 overflow-hidden rounded-md border-8 border-transparent bg-[--astro-code-color-background] bg-clip-border p-2 pb-4;
45+
@apply mt-4 overflow-clip rounded-md border-8 border-transparent bg-[--astro-code-color-background] bg-clip-border p-2 pb-4;
4646
}
4747
.pp-app-container {
4848
@apply border-0 border-t-2 border-dashed border-zinc-400 px-2 pt-5 dark:border-sky-700;

packages/docs/src/components/playgrounds/pardon/PardonPlaygroundLoader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default function PardonPlaygroundLoader(
7575
);
7676

7777
return (
78-
<div class="pp-container grid w-full gap-2">
78+
<div class="pp-container grid gap-2">
7979
<div>{props.children}</div>
8080
<div class="pp-app-container not-content !mt-0">
8181
<Suspense fallback={loading}>

packages/docs/src/content/docs/intro/collections.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ endpoint=example/products/list
297297
you can try different endpoints, and/or `env=stage` if you like,
298298
for the get/update/delete endpoints, you'll need to specify a
299299
`product=...` value as well.
300+
:::note
301+
This only works for `GET` method endpoints.
302+
303+
Non-GET endpoints need the `method` specified as well, this
304+
adds a little friction to prevent accidental `POST` requests.
305+
306+
<IconicCode name="setting">
307+
```
308+
endpoint=example/products/create method=POST name=thneed
309+
```
310+
</IconicCode>
311+
:::
300312
</ExerciseItem>
301313
</Exercises>
302314
</PardonPlayground>

packages/docs/src/content/docs/intro/scripting.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Authorization: {{ @auth = serviceToken(env) }}
118118
</Tabs>
119119

120120
<PardonPlayground values secrets={'shown'} config={serviceAndPingAndProductsWithConfigurableScriptAuth} example={`
121-
GET https://example.com/products
121+
GET https://example.com/products
122122
`}>
123123
Feel free to experiment with the usual `env=stage`/`env=prod` changes,
124124
(notice that pardon takes a moment to update now.)
@@ -216,7 +216,7 @@ This price script isn't editable here (it's hacked into the demo system).
216216
</Tabs>
217217

218218
<PardonPlayground response data values='product=P1001' secrets='shown' editor='example/products/get.https' example={`
219-
GET https://example.com/products/{{product}}
219+
GET https://example.com/products/{{product}}
220220
`} config={{ ...serviceWithOrderingAndGetMatching, 'example/products/config.yaml': '' }}>
221221
<Exercises>
222222
<ExerciseItem label='Dynamic data'
@@ -315,8 +315,9 @@ We have one more thing to fix, though. The `price` is per-unit
315315
but the `cost` needs to be the `price` multiplied by the `quantity` for each item.
316316

317317
<PardonPlayground response data values='items=[{ product: P1001 }, { product: P1003 }]'
318-
secrets='shown' editor='example/orders/create.https' example={`
319-
POST https://example.com/orders
318+
secrets='shown' editor='example/orders/create.https'
319+
example={`
320+
POST https://example.com/orders
320321
`} config={serviceWithAutoCost}>
321322
<Exercises>
322323
<ExerciseItem label="setup quantities"

packages/docs/src/content/docs/intro/templates.mdx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,13 @@ Anyway, let's explore how pardon behaves with this template with these quick exe
316316

317317
<p/>
318318
<PardonPlayground example={`
319-
POST https://example.com/products
319+
POST https://example.com/products
320320
321-
{
322-
"name": "sample",
323-
"price": 9.99
324-
}`} config={productsExample}>
321+
{
322+
"name": "sample",
323+
"price": 9.99
324+
}
325+
`} config={productsExample}>
325326

326327
<Exercises>
327328
<ExerciseItem label="Changing values"
@@ -419,12 +420,13 @@ For example, since we have a value for `{{name}}` matched by the template, we
419420
can use the `name` value in an expression for another field.
420421

421422
<PardonPlayground example={`
422-
POST https://example.com/products
423+
POST https://example.com/products
423424
424-
{
425-
"name": "pay-by-the-letter",
426-
"price": "{{ price = name.length * 10 }}"
427-
}`}
425+
{
426+
"name": "pay-by-the-letter",
427+
"price": "{{ price = name.length * 10 }}"
428+
}
429+
`}
428430
config={productsExample}>
429431

430432
<Exercises>

packages/docs/src/content/docs/intro/testcases.mdx

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ each `each`.
433433
We should explore this behavior interactively.
434434

435435
<TestcasePlayground example={`
436-
set("env", "stage");
437-
set("name", "pens");
436+
set("env", "stage");
437+
set("name", "pens");
438438
`}>
439439
<Exercises>
440440
<ExerciseItem
@@ -632,13 +632,13 @@ with the environment `{ env: "stage", name: "pencils" }`.
632632
Let's experiment with this structure to get a feel for it.
633633

634634
<TestcasePlayground mode="trials" example={`
635-
cases(({ set }) => {
636-
set({ env: "stage", name: "pencils" });
637-
});
635+
cases(({ set }) => {
636+
set({ env: "stage", name: "pencils" });
637+
});
638638
639-
trial("%env/get-product-%name", ({ env, name }) => {
640-
/* ... */
641-
});
639+
trial("%env/get-product-%name", ({ env, name }) => {
640+
/* ... */
641+
});
642642
`.trim()}>
643643
<Exercises>
644644
<ExerciseItem
@@ -686,16 +686,16 @@ Starting with our 6 cases, we can add `stop({ env: "prod", name: "pens" })` to r
686686
the production test cases involving `pens`.
687687

688688
<TestcasePlayground mode="trials" example={`
689-
cases(({ set, each }) => {
690-
set({
691-
env: each("stage", "prod"),
692-
name: each("pencils", "pens", "markers")
689+
cases(({ set, each }) => {
690+
set({
691+
env: each("stage", "prod"),
692+
name: each("pencils", "pens", "markers")
693+
});
693694
});
694-
});
695695
696-
trial("%env/get-product-%name", ({ env, name }) => {
697-
/* ... */
698-
});
696+
trial("%env/get-product-%name", ({ env, name }) => {
697+
/* ... */
698+
});
699699
`.trim()}>
700700
<Exercises>
701701
<ExerciseItem
@@ -763,7 +763,7 @@ gamut(() => {
763763
set("name", each("pencils", "pens"));
764764
});
765765

766-
trial("%env/get-product-%name", () => {
766+
trial("%env/get-product-%name", () => {
767767
/*...*/
768768
});
769769
});
@@ -784,19 +784,19 @@ There are many utilities defining transforms of the test case(s):
784784
- etc...
785785

786786
<TestcasePlayground mode="trials" example={`
787-
cases(({ each, defi }) => {
788-
defi("env", each("stage", "prod"), "local");
789-
});
790-
791-
gamut("%env", () => {
792-
cases(({ set, each }) => {
793-
set("name", each("pencils", "pens"));
787+
cases(({ each, defi }) => {
788+
defi("env", each("stage", "prod"), "local");
794789
});
795790
796-
trial("get-product-%name", () => {
797-
/*...*/
791+
gamut("%env", () => {
792+
cases(({ set, each }) => {
793+
set("name", each("pencils", "pens"));
794+
});
795+
796+
trial("get-product-%name", () => {
797+
/*...*/
798+
});
798799
});
799-
});
800800
`.trim()}>
801801
</TestcasePlayground>
802802

@@ -824,12 +824,12 @@ And the shuffle is just `~2` or some other number, with a value of `~0` for not
824824
Understandably, this is best understood by experience:
825825

826826
<TestcasePlayground smoker={["", "name", "2,env,name", "name%env", "2,name%env", "name:2~0", "env:3,name:2", "env:3,name:2~0"]} example={`
827-
set({
828-
env: each("stage", "prod"),
829-
name: each("pencils", "pens", "markers"),
830-
case: each(..."abcd"),
831-
subcase: each(..."xyzw"),
832-
});
827+
set({
828+
env: each("stage", "prod"),
829+
name: each("pencils", "pens", "markers"),
830+
case: each(..."abcd"),
831+
subcase: each(..."xyzw"),
832+
});
833833
`}>
834834
<Exercises>
835835
<ExerciseItem label="Smoke testing"

0 commit comments

Comments
 (0)