Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
chore: remove prettier, just use strings
Browse files Browse the repository at this point in the history
  • Loading branch information
hanspagel committed Dec 12, 2023
1 parent 083087c commit fd7dbbc
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 97 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://discord.gg/8HeZcRGPFS>
Expand Down
1 change: 0 additions & 1 deletion packages/snippetz-plugin-undici/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"vitest": "^1.0.4"
},
"dependencies": {
"prettier": "^3.1.1",
"@scalar/snippetz": "workspace:*"
},
"files": [
Expand Down
Binary file not shown.
54 changes: 24 additions & 30 deletions packages/snippetz-plugin-undici/src/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@ 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', () => {
const source = 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')
`)
})

Expand All @@ -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'
})
`)
})

Expand All @@ -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'
}
})
`)
})

Expand All @@ -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'
}
})
`)
})

Expand All @@ -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'
}
})
`)
})

Expand All @@ -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'
}
})
`)
})
})
13 changes: 9 additions & 4 deletions packages/snippetz-plugin-undici/src/undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function arrayToObject(items: any) {
}, {})
}

export function formatAsJavaScriptObject(data: Record<string, any>) {
return JSON.stringify(data, null, 2)
.replaceAll(`'`, `\'`)
.replaceAll(`"`, `'`)
}

export function undici(request: Partial<Request>): Source {
// Defaults
const normalizedRequest = {
Expand Down Expand Up @@ -73,14 +79,13 @@ export function undici(request: Partial<Request>): 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
Expand Down
1 change: 0 additions & 1 deletion packages/snippetz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"vitest": "^1.0.4"
},
"dependencies": {
"prettier": "^3.1.1"
},
"files": [
"dist"
Expand Down
Binary file added packages/snippetz/scalar-snippetz-0.0.0.tgz
Binary file not shown.
13 changes: 0 additions & 13 deletions packages/snippetz/src/format.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/snippetz/src/format.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/snippetz/src/snippetz.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
3 changes: 1 addition & 2 deletions packages/snippetz/src/snippetz.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { format } from './format'
import type { Source } from './types'

export type SnippetOptions = {}
Expand All @@ -13,7 +12,7 @@ export function snippetz() {
...options,
}

return format(source)
return source.code
},
}
}
13 changes: 0 additions & 13 deletions pnpm-lock.yaml

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

3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down

0 comments on commit fd7dbbc

Please sign in to comment.