diff --git a/README.md b/README.md index 112bf31..d65157f 100644 --- a/README.md +++ b/README.md @@ -51,17 +51,6 @@ const source = undici({ console.log(source.code) ``` -### Format the source code - -```js -import { format } from '@scalar/snippetz' - -console.log(format({ - target: 'js', - code: `const answer= 42 ;` -})) -``` - ## Community We are API nerds. You too? Let’s chat on Discord: diff --git a/packages/snippetz-plugin-undici/package.json b/packages/snippetz-plugin-undici/package.json index d76b2a7..746e452 100644 --- a/packages/snippetz-plugin-undici/package.json +++ b/packages/snippetz-plugin-undici/package.json @@ -17,7 +17,6 @@ "vitest": "^1.0.4" }, "dependencies": { - "prettier": "^3.1.1", "@scalar/snippetz": "workspace:*" }, "files": [ diff --git a/packages/snippetz-plugin-undici/scalar-snippetz-plugin-undici-0.0.0.tgz b/packages/snippetz-plugin-undici/scalar-snippetz-plugin-undici-0.0.0.tgz new file mode 100644 index 0000000..07cff95 Binary files /dev/null and b/packages/snippetz-plugin-undici/scalar-snippetz-plugin-undici-0.0.0.tgz differ diff --git a/packages/snippetz-plugin-undici/src/undici.test.ts b/packages/snippetz-plugin-undici/src/undici.test.ts index adee068..8692a10 100644 --- a/packages/snippetz-plugin-undici/src/undici.test.ts +++ b/packages/snippetz-plugin-undici/src/undici.test.ts @@ -7,7 +7,7 @@ describe('undici', () => { url: 'https://example.com', }) - expect(source.code).toContain(`import { request } from "undici"`) + expect(source.code).toContain(`import { request } from 'undici'`) }) it('returns a basic request', () => { @@ -15,10 +15,9 @@ describe('undici', () => { url: 'https://example.com', }) - expect(source.code).toBe(`import { request } from "undici" - -const { statusCode, headers, body } = await request("https://example.com") + expect(source.code).toBe(`import { request } from 'undici' +const { statusCode, headers, body } = await request('https://example.com') `) }) @@ -28,12 +27,11 @@ const { statusCode, headers, body } = await request("https://example.com") method: 'post', }) - expect(source.code).toBe(`import { request } from "undici" + expect(source.code).toBe(`import { request } from 'undici' -const { statusCode, headers, body } = await request("https://example.com", { - "method": "POST" +const { statusCode, headers, body } = await request('https://example.com', { + 'method': 'POST' }) - `) }) @@ -48,14 +46,13 @@ const { statusCode, headers, body } = await request("https://example.com", { ], }) - expect(source.code).toBe(`import { request } from "undici" + expect(source.code).toBe(`import { request } from 'undici' -const { statusCode, headers, body } = await request("https://example.com", { - "headers": { - "Content-Type": "application/json" +const { statusCode, headers, body } = await request('https://example.com', { + 'headers': { + 'Content-Type': 'application/json' } }) - `) }) @@ -76,17 +73,16 @@ const { statusCode, headers, body } = await request("https://example.com", { }, }) - expect(source.code).toBe(`import { request } from "undici" + expect(source.code).toBe(`import { request } from 'undici' -const { statusCode, headers, body } = await request("https://example.com", { - "headers": { - "Content-Type": "application/json" +const { statusCode, headers, body } = await request('https://example.com', { + 'headers': { + 'Content-Type': 'application/json' }, - "body": { - "hello": "world" + 'body': { + 'hello': 'world' } }) - `) }) @@ -111,14 +107,13 @@ const { statusCode, headers, body } = await request("https://example.com", { ], }) - expect(source.code).toBe(`import { request } from "undici" + expect(source.code).toBe(`import { request } from 'undici' -const { statusCode, headers, body } = await request("https://example.com?foo=bar&bar=foo", { - "headers": { - "Content-Type": "application/json" +const { statusCode, headers, body } = await request('https://example.com?foo=bar&bar=foo', { + 'headers': { + 'Content-Type': 'application/json' } }) - `) }) @@ -137,14 +132,13 @@ const { statusCode, headers, body } = await request("https://example.com?foo=bar ], }) - expect(source.code).toBe(`import { request } from "undici" + expect(source.code).toBe(`import { request } from 'undici' -const { statusCode, headers, body } = await request("https://example.com", { - "headers": { - "Set-Cookie": "foo=bar; bar=foo" +const { statusCode, headers, body } = await request('https://example.com', { + 'headers': { + 'Set-Cookie': 'foo=bar; bar=foo' } }) - `) }) }) diff --git a/packages/snippetz-plugin-undici/src/undici.ts b/packages/snippetz-plugin-undici/src/undici.ts index 97b06ba..ba3af24 100644 --- a/packages/snippetz-plugin-undici/src/undici.ts +++ b/packages/snippetz-plugin-undici/src/undici.ts @@ -9,6 +9,12 @@ function arrayToObject(items: any) { }, {}) } +export function formatAsJavaScriptObject(data: Record) { + return JSON.stringify(data, null, 2) + .replaceAll(`'`, `\'`) + .replaceAll(`"`, `'`) +} + export function undici(request: Partial): Source { // Defaults const normalizedRequest = { @@ -73,14 +79,13 @@ export function undici(request: Partial): Source { // Transform to JSON const jsonOptions = Object.keys(options).length - ? `, ${JSON.stringify(options, null, 2)}` + ? `, ${formatAsJavaScriptObject(options)}` : '' // Code Template - const code = `import { request } from "undici" - -const { statusCode, headers, body } = await request("${normalizedRequest.url}${queryString}"${jsonOptions}) + const code = `import { request } from 'undici' +const { statusCode, headers, body } = await request('${normalizedRequest.url}${queryString}'${jsonOptions}) ` // Create an AST diff --git a/packages/snippetz/package.json b/packages/snippetz/package.json index d5778e3..8787b49 100644 --- a/packages/snippetz/package.json +++ b/packages/snippetz/package.json @@ -15,7 +15,6 @@ "vitest": "^1.0.4" }, "dependencies": { - "prettier": "^3.1.1" }, "files": [ "dist" diff --git a/packages/snippetz/scalar-snippetz-0.0.0.tgz b/packages/snippetz/scalar-snippetz-0.0.0.tgz new file mode 100644 index 0000000..aee2df9 Binary files /dev/null and b/packages/snippetz/scalar-snippetz-0.0.0.tgz differ diff --git a/packages/snippetz/src/format.test.ts b/packages/snippetz/src/format.test.ts deleted file mode 100644 index c92acbc..0000000 --- a/packages/snippetz/src/format.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { expect, describe, it } from 'vitest' -import { format } from './format' - -describe('format', async () => { - it('formats basic JS', async () => { - expect( - await format({ - target: 'javascript', - code: 'const answer=42', - }) - ).toBe(`const answer = 42\n`) - }) -}) diff --git a/packages/snippetz/src/format.ts b/packages/snippetz/src/format.ts deleted file mode 100644 index e14c038..0000000 --- a/packages/snippetz/src/format.ts +++ /dev/null @@ -1,18 +0,0 @@ -import babel from 'prettier/plugins/babel' -import estree from 'prettier/plugins/estree' -import * as prettier from 'prettier' - -export async function format(source: any) { - const target = source.target - - if (target === 'javascript') { - return await prettier.format(source.code, { - semi: false, - parser: 'babel', - plugins: [babel, estree], - singleQuote: true, - }) - } - - throw new Error(`Unsupported target: ${source.target}`) -} diff --git a/packages/snippetz/src/snippetz.test.ts b/packages/snippetz/src/snippetz.test.ts index fcf9de2..769237d 100644 --- a/packages/snippetz/src/snippetz.test.ts +++ b/packages/snippetz/src/snippetz.test.ts @@ -3,9 +3,9 @@ import { snippetz } from './snippetz' describe('snippetz', async () => { it('formats basic JS', async () => { - const snippet = await snippetz().get({ + const snippet = snippetz().get({ target: 'javascript', - code: 'const answer=42', + code: 'const answer = 42\n', }) expect(snippet).toBe(`const answer = 42\n`) diff --git a/packages/snippetz/src/snippetz.ts b/packages/snippetz/src/snippetz.ts index 83c9bda..fa1b5ba 100644 --- a/packages/snippetz/src/snippetz.ts +++ b/packages/snippetz/src/snippetz.ts @@ -1,4 +1,3 @@ -import { format } from './format' import type { Source } from './types' export type SnippetOptions = {} @@ -13,7 +12,7 @@ export function snippetz() { ...options, } - return format(source) + return source.code }, } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c614667..c901d38 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,10 +47,6 @@ importers: version: 1.8.25(typescript@5.3.3) packages/snippetz: - dependencies: - prettier: - specifier: ^3.1.1 - version: 3.1.1 devDependencies: '@vitest/ui': specifier: ^1.0.4 @@ -70,9 +66,6 @@ importers: '@scalar/snippetz': specifier: workspace:* version: link:../snippetz - prettier: - specifier: ^3.1.1 - version: 3.1.1 devDependencies: '@types/har-format': specifier: ^1.2.15 @@ -2416,12 +2409,6 @@ packages: hasBin: true dev: true - /prettier@3.1.1: - resolution: {integrity: sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw==} - engines: {node: '>=14'} - hasBin: true - dev: false - /pretty-format@29.7.0: resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} diff --git a/tsconfig.json b/tsconfig.json index 1ba2bf5..8ca4130 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,12 +3,11 @@ "target": "ES2020", "useDefineForClassFields": true, "module": "ESNext", - "lib": ["ES2020", "DOM", "DOM.Iterable"], + "lib": ["ES2021", "DOM", "DOM.Iterable"], "skipLibCheck": true, /* Bundler mode */ "moduleResolution": "bundler", - "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true,