Skip to content

Commit

Permalink
feat: return json data from curl request (closes #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
uncenter committed Feb 9, 2024
1 parent c8462db commit 5a6b811
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
node_modules/
dist/

.wrangler/
.cache/
.env

Expand Down
4 changes: 4 additions & 0 deletions config/11ty/filters.js
@@ -1,5 +1,6 @@
import { DateTime } from 'luxon';
import urlize from '../urlize.js';
import stringify from '../json-pretty-stringify.js';

const toShortDate = (dateObject) => {
// 10/14/1983
Expand Down Expand Up @@ -51,5 +52,8 @@ export const filters = (eleventyConfig) => {
eleventyConfig.addFilter('capitalize', (string) => {
return string.charAt(0).toUpperCase() + string.slice(1);
});
eleventyConfig.addFilter('stringify', (value) => {
return stringify(value, '\t');
});
eleventyConfig.addFilter('url', urlize);
};
28 changes: 28 additions & 0 deletions config/json-pretty-stringify.js
@@ -0,0 +1,28 @@
export default function (input, space) {
return JSON.stringify(
input,
function (k, v) {
if (Array.isArray(v)) {
let children = '';

for (let index = 0, l = v.length; index < l; index++) {
const value = v[index];
const child = JSON.stringify(value);

if (child === undefined) continue;

children += children ? `, ${child}` : child;
}

return `[${children}]`;
}
return v;
},
space,
)
.replaceAll('\\', '')
.replaceAll('"[', '[')
.replaceAll(']"', ']')
.replaceAll('"{', '{')
.replaceAll('}"', '}');
}
22 changes: 22 additions & 0 deletions functions/index.ts
@@ -0,0 +1,22 @@
import stringify from '../config/json-pretty-stringify.js';
import whoami from '../src/_data/whoami.js';

// @ts-expect-error
export async function onRequest(context: EventContext): PagesFunction {
const { next, request } = context as {
next: (
input?: Request | string,
init?: RequestInit,
) => Promise<Response>;
request: Request;
};

if (
new URL(request.url).pathname === '/' &&
request.headers.get('user-agent')?.startsWith('curl/')
) {
return new Response(stringify(whoami, 2));
}

return await next();
}
7 changes: 7 additions & 0 deletions functions/package.json
@@ -0,0 +1,7 @@
{
"name": "functions",
"version": "0.1.0",
"devDependencies": {
"@cloudflare/workers-types": "^4.20240208.0"
}
}
16 changes: 16 additions & 0 deletions functions/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions functions/tsconfig.json
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"noEmit": true,
"module": "esnext",
"target": "esnext",
"lib": ["esnext"],
"strict": true,
"alwaysStrict": true,
"preserveConstEnums": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"sourceMap": true,
"types": ["@cloudflare/workers-types"]
},
"include": ["src"],
"exclude": ["node_modules", "dist", "test"]
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -58,5 +58,5 @@
"prettier": "^3.2.5",
"rimraf": "^5.0.5"
},
"packageManager": "pnpm@8.9.0"
"packageManager": "pnpm@8.15.1"
}
12 changes: 12 additions & 0 deletions src/_data/whoami.js
@@ -0,0 +1,12 @@
import site from '../../site.config.js';

export default {
name: site.author.name,
website: site.url,
occupation: 'Student',
location: 'United States',
languages: ['JavaScript', 'TypeScript', 'Rust', 'Python', 'HTML', 'CSS'],
frameworks: ['SolidJS', 'Eleventy', 'TailwindCSS'],
interests: ['Web Development', 'Software Development', 'Open Source'],
learning: ['Rust', 'Vue', 'Fortran', 'Swift'],
};
11 changes: 1 addition & 10 deletions src/index.njk
Expand Up @@ -11,16 +11,7 @@ templateEngineOverride: njk, md
<div class="py-4">

```js
const whoami = {
name: "{{ site.author.name }}",
website: "{{ site.url }}",
occupation: "Student",
location: "United States",
languages: ["JavaScript", "TypeScript", "Rust", "Python", "HTML", "CSS"],
frameworks: ["SolidJS", "Eleventy", "TailwindCSS"],
interests: ["Web Development", "Software Development", "Open Source"],
learning: ["Rust", "Vue", "Fortran", "Swift"],
};
const whoami = {{ whoami | stringify | safe }};
```

</div>

0 comments on commit 5a6b811

Please sign in to comment.