Skip to content

Commit 898134d

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

File tree

11 files changed

+128
-85
lines changed

11 files changed

+128
-85
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/PardonPlaygroundDataView.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,24 @@ export default function PardonPlaygroundDataView(
5555
}
5656

5757
if (response?.result) {
58+
const {
59+
inbound: { values, secrets },
60+
} = response.result;
5861
return {
59-
data: response.result?.inbound,
62+
data: { values, secrets },
6063
};
6164
}
6265

6366
const { execution } = executionHandle;
6467

6568
try {
66-
const request = await execution.outbound;
69+
const {
70+
request: { values: secrets },
71+
redacted: { values },
72+
} = await execution.outbound;
6773

6874
return {
69-
data: request,
75+
data: { values, secrets },
7076
};
7177
} catch (error) {
7278
return {
@@ -106,7 +112,7 @@ export default function PardonPlaygroundDataView(
106112

107113
return (
108114
<CodeMirror
109-
class="max-h-56 overflow-y-auto rounded-md p-2 text-blue-800 dark:bg-stone-700"
115+
class="max-h-56 overflow-y-auto rounded-md p-2 text-blue-800 dark:bg-stone-700 dark:text-blue-200"
110116
classList={{
111117
"text-yellow-900 dark:text-purple-300": Boolean(!response()),
112118
"text-green-800 dark:text-green-500": Boolean(response()),

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/dataflow.mdx

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ You can reset the system with the reset button there too.
9999
:::
100100

101101
<PardonPlayground response data example={`
102-
POST https://example.com/products
102+
POST https://example.com/products
103103
104-
{
105-
"name": "pencils",
106-
"price": 2
107-
}
104+
{
105+
"name": "pencils",
106+
"price": 2
107+
}
108108
`} config={serviceAndPingAndProductsWithConfigurableAuth}>
109109
<Exercises>
110110
<ExerciseItem label='add pencils'
@@ -156,11 +156,12 @@ Before we introduce structured data and scopes, we just tneed to see
156156
that values can conflict if they are specified to two values.
157157

158158
<PardonPlayground values="name=abc" example={`
159-
POST https://example.com/products
159+
POST https://example.com/products
160160
161-
{
162-
"name": "abc"
163-
}`} config={serviceAndPingAndProductsWithConfigurableAuth}>
161+
{
162+
"name": "abc"
163+
}
164+
`} config={serviceAndPingAndProductsWithConfigurableAuth}>
164165

165166
Try changing the value of name in either the values or the input request here.
166167
See how pardon becomes confused
@@ -189,13 +190,13 @@ Pardon interprets a single element array as a template for each item,
189190
and then evaluates each item in its own scope. Let's see how this works here
190191

191192
<PardonPlayground values example={`
192-
POST https://example.com/orders
193+
POST https://example.com/orders
193194
194-
{
195-
"cart": [
196-
{ "product": "P1001" }
197-
]
198-
}
195+
{
196+
"cart": [
197+
{ "product": "P1001" }
198+
]
199+
}
199200
`} config={serviceWithOrdering}>
200201
<Exercises>
201202
<ExerciseItem label="Add an item" prompt="Add a cart item">
@@ -275,14 +276,14 @@ This means replacing `{{product}}` with `{{items.product}}` and
275276
name for the scoped values.
276277

277278
<PardonPlayground values editor="example/orders/create.https" data example={`
278-
POST https://example.com/orders
279-
280-
{
281-
"cart": [
282-
{ "product": "P1001", "quantity": 7 },
283-
{ "product": "P1002", "quantity": 42 }
284-
]
285-
}
279+
POST https://example.com/orders
280+
281+
{
282+
"cart": [
283+
{ "product": "P1001", "quantity": 7 },
284+
{ "product": "P1002", "quantity": 42 }
285+
]
286+
}
286287
`} config={serviceWithOrdering}>
287288

288289
<Exercises>
@@ -530,7 +531,7 @@ is clearly a new element based on the `id`.
530531
This mapping also affects export scopes.
531532

532533
<PardonPlayground response data editor="example/products/list.https" example={`
533-
GET https://example.com/products
534+
GET https://example.com/products
534535
`} config={serviceWithOrdering}>
535536
<Exercises>
536537
<ExerciseItem label="match response"
@@ -628,7 +629,7 @@ a request URL) is handled as a simple map, but we can turn on multi-map function
628629
Let's do that here,... just to get a feel for things.
629630

630631
<PardonPlayground editor="example/ping.https" example={`
631-
GET https://example.com/ping
632+
GET https://example.com/ping
632633
`} config={serviceAndPingAndProducts}>
633634
<Exercises>
634635
<ExerciseItem label="two params"

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>

0 commit comments

Comments
 (0)