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 898134d
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 85 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 @@ -55,18 +55,24 @@ export default function PardonPlaygroundDataView(
}

if (response?.result) {
const {
inbound: { values, secrets },
} = response.result;
return {
data: response.result?.inbound,
data: { values, secrets },
};
}

const { execution } = executionHandle;

try {
const request = await execution.outbound;
const {
request: { values: secrets },
redacted: { values },
} = await execution.outbound;

return {
data: request,
data: { values, secrets },
};
} catch (error) {
return {
Expand Down Expand Up @@ -106,7 +112,7 @@ export default function PardonPlaygroundDataView(

return (
<CodeMirror
class="max-h-56 overflow-y-auto rounded-md p-2 text-blue-800 dark:bg-stone-700"
class="max-h-56 overflow-y-auto rounded-md p-2 text-blue-800 dark:bg-stone-700 dark:text-blue-200"
classList={{
"text-yellow-900 dark:text-purple-300": Boolean(!response()),
"text-green-800 dark:text-green-500": Boolean(response()),
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
51 changes: 26 additions & 25 deletions packages/docs/src/content/docs/intro/dataflow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ You can reset the system with the reset button there too.
:::

<PardonPlayground response data example={`
POST https://example.com/products
POST https://example.com/products
{
"name": "pencils",
"price": 2
}
{
"name": "pencils",
"price": 2
}
`} config={serviceAndPingAndProductsWithConfigurableAuth}>
<Exercises>
<ExerciseItem label='add pencils'
Expand Down Expand Up @@ -156,11 +156,12 @@ Before we introduce structured data and scopes, we just tneed to see
that values can conflict if they are specified to two values.

<PardonPlayground values="name=abc" example={`
POST https://example.com/products
POST https://example.com/products
{
"name": "abc"
}`} config={serviceAndPingAndProductsWithConfigurableAuth}>
{
"name": "abc"
}
`} config={serviceAndPingAndProductsWithConfigurableAuth}>

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

<PardonPlayground values example={`
POST https://example.com/orders
POST https://example.com/orders
{
"cart": [
{ "product": "P1001" }
]
}
{
"cart": [
{ "product": "P1001" }
]
}
`} config={serviceWithOrdering}>
<Exercises>
<ExerciseItem label="Add an item" prompt="Add a cart item">
Expand Down Expand Up @@ -275,14 +276,14 @@ This means replacing `{{product}}` with `{{items.product}}` and
name for the scoped values.

<PardonPlayground values editor="example/orders/create.https" data example={`
POST https://example.com/orders
{
"cart": [
{ "product": "P1001", "quantity": 7 },
{ "product": "P1002", "quantity": 42 }
]
}
POST https://example.com/orders
{
"cart": [
{ "product": "P1001", "quantity": 7 },
{ "product": "P1002", "quantity": 42 }
]
}
`} config={serviceWithOrdering}>

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

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

<PardonPlayground editor="example/ping.https" example={`
GET https://example.com/ping
GET https://example.com/ping
`} config={serviceAndPingAndProducts}>
<Exercises>
<ExerciseItem label="two params"
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
Loading

0 comments on commit 898134d

Please sign in to comment.