Skip to content

Commit

Permalink
core: accept method in values + doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sparecycles committed Sep 12, 2024
1 parent 8df740f commit 093421a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 56 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/core/pardon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ export const PardonFetchExecution = pardonExecution({
async match({ context: { url, init, ...context } }) {
const fetchObject = fetchIntoObject(url, init);

if (typeof context.values?.method === "string") {
fetchObject.method ??= context.values?.method;

if (context.values?.method !== fetchObject.method) {
throw new Error(
"specified values method does not match reqeust method",
);
}
}

// pathname undefined in some places is allowed (matches any template),
// but we want to ensure it's set when matching requests.
// but allow undefined origin and pathname for undefined URLs matched/rendered only by values.
Expand Down
23 changes: 17 additions & 6 deletions packages/core/src/entry/main/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@ export async function processOptions(
method = args.shift()!;
}

if (values.method !== undefined && typeof values.method !== "string") {
throw new Error("method should be a string");
}

if (!args.length) {
return { values, init: { method: method ?? "GET" } };
return {
values,
init: { method: (method ?? values.method ?? "GET") as string },
};
}

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

request.method ??= method;
if (typeof values.method !== "string") {
throw new Error("method in values must be a string.");
}

request.method ??= method ?? values.method ?? "GET";

if (values.method && values.method && values.method !== request.method) {
throw new Error("method in input does not match method in request");
}

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

if (!values.endpoint && !values.method) {
request.method ??= method ?? "GET";
}

return { url: intoURL(request), init: request, values };
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/components/Exercises.astro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const props = Astro.props;

<div
{...props}
class={"relative border border-gray-500 p-3 pt-0" + (props.class ?? "")}
class={"relative border border-gray-500 p-3 pt-0 " + (props.class ?? "")}
>
<div
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"
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/components/PardonPlayground.astro
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const { secrets, editor, values, data, response, ...props } = Astro.props;

<style is:global>
.pp-container {
@apply mt-4 overflow-hidden rounded-md border-8 border-transparent bg-[--astro-code-color-background] bg-clip-border p-2 pb-4;
@apply mt-4 overflow-clip rounded-md border-8 border-transparent bg-[--astro-code-color-background] bg-clip-border p-2 pb-4;
}
.pp-app-container {
@apply border-0 border-t-2 border-dashed border-zinc-400 px-2 pt-5 dark:border-sky-700;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function PardonPlaygroundLoader(
);

return (
<div class="pp-container grid w-full gap-2">
<div class="pp-container grid gap-2">
<div>{props.children}</div>
<div class="pp-app-container not-content !mt-0">
<Suspense fallback={loading}>
Expand Down
12 changes: 12 additions & 0 deletions packages/docs/src/content/docs/intro/collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,18 @@ endpoint=example/products/list
you can try different endpoints, and/or `env=stage` if you like,
for the get/update/delete endpoints, you'll need to specify a
`product=...` value as well.
:::note
This only works for `GET` method endpoints.

Non-GET endpoints need the `method` specified as well, this
adds a little friction to prevent accidental `POST` requests.

<IconicCode name="setting">
```
endpoint=example/products/create method=POST name=thneed
```
</IconicCode>
:::
</ExerciseItem>
</Exercises>
</PardonPlayground>
Expand Down
9 changes: 5 additions & 4 deletions packages/docs/src/content/docs/intro/scripting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Authorization: {{ @auth = serviceToken(env) }}
</Tabs>

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

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

<PardonPlayground response data values='items=[{ product: P1001 }, { product: P1003 }]'
secrets='shown' editor='example/orders/create.https' example={`
POST https://example.com/orders
secrets='shown' editor='example/orders/create.https'
example={`
POST https://example.com/orders
`} config={serviceWithAutoCost}>
<Exercises>
<ExerciseItem label="setup quantities"
Expand Down
22 changes: 12 additions & 10 deletions packages/docs/src/content/docs/intro/templates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,13 @@ Anyway, let's explore how pardon behaves with this template with these quick exe

<p/>
<PardonPlayground example={`
POST https://example.com/products
POST https://example.com/products
{
"name": "sample",
"price": 9.99
}`} config={productsExample}>
{
"name": "sample",
"price": 9.99
}
`} config={productsExample}>

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

<PardonPlayground example={`
POST https://example.com/products
POST https://example.com/products
{
"name": "pay-by-the-letter",
"price": "{{ price = name.length * 10 }}"
}`}
{
"name": "pay-by-the-letter",
"price": "{{ price = name.length * 10 }}"
}
`}
config={productsExample}>

<Exercises>
Expand Down
66 changes: 33 additions & 33 deletions packages/docs/src/content/docs/intro/testcases.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ each `each`.
We should explore this behavior interactively.

<TestcasePlayground example={`
set("env", "stage");
set("name", "pens");
set("env", "stage");
set("name", "pens");
`}>
<Exercises>
<ExerciseItem
Expand Down Expand Up @@ -632,13 +632,13 @@ with the environment `{ env: "stage", name: "pencils" }`.
Let's experiment with this structure to get a feel for it.

<TestcasePlayground mode="trials" example={`
cases(({ set }) => {
set({ env: "stage", name: "pencils" });
});
cases(({ set }) => {
set({ env: "stage", name: "pencils" });
});
trial("%env/get-product-%name", ({ env, name }) => {
/* ... */
});
trial("%env/get-product-%name", ({ env, name }) => {
/* ... */
});
`.trim()}>
<Exercises>
<ExerciseItem
Expand Down Expand Up @@ -686,16 +686,16 @@ Starting with our 6 cases, we can add `stop({ env: "prod", name: "pens" })` to r
the production test cases involving `pens`.

<TestcasePlayground mode="trials" example={`
cases(({ set, each }) => {
set({
env: each("stage", "prod"),
name: each("pencils", "pens", "markers")
cases(({ set, each }) => {
set({
env: each("stage", "prod"),
name: each("pencils", "pens", "markers")
});
});
});
trial("%env/get-product-%name", ({ env, name }) => {
/* ... */
});
trial("%env/get-product-%name", ({ env, name }) => {
/* ... */
});
`.trim()}>
<Exercises>
<ExerciseItem
Expand Down Expand Up @@ -763,7 +763,7 @@ gamut(() => {
set("name", each("pencils", "pens"));
});

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

<TestcasePlayground mode="trials" example={`
cases(({ each, defi }) => {
defi("env", each("stage", "prod"), "local");
});
gamut("%env", () => {
cases(({ set, each }) => {
set("name", each("pencils", "pens"));
cases(({ each, defi }) => {
defi("env", each("stage", "prod"), "local");
});
trial("get-product-%name", () => {
/*...*/
gamut("%env", () => {
cases(({ set, each }) => {
set("name", each("pencils", "pens"));
});
trial("get-product-%name", () => {
/*...*/
});
});
});
`.trim()}>
</TestcasePlayground>

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

<TestcasePlayground smoker={["", "name", "2,env,name", "name%env", "2,name%env", "name:2~0", "env:3,name:2", "env:3,name:2~0"]} example={`
set({
env: each("stage", "prod"),
name: each("pencils", "pens", "markers"),
case: each(..."abcd"),
subcase: each(..."xyzw"),
});
set({
env: each("stage", "prod"),
name: each("pencils", "pens", "markers"),
case: each(..."abcd"),
subcase: each(..."xyzw"),
});
`}>
<Exercises>
<ExerciseItem label="Smoke testing"
Expand Down

0 comments on commit 093421a

Please sign in to comment.