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

Commit

Permalink
refactor: use custom object to string function, instead of JSON.strin…
Browse files Browse the repository at this point in the history
…gify
  • Loading branch information
hanspagel committed Dec 12, 2023
1 parent 9d4fedd commit 984b039
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const source = undici({
})

console.log(source.code)

// import { request } from 'undici'

// const { statusCode, headers, body } = await request(
// 'url': 'https://example.com',
// )
```

## Community
Expand Down
14 changes: 7 additions & 7 deletions packages/snippetz-plugin-undici/src/undici.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ 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', {
'method': 'POST'
method: 'POST'
})
`)
})
Expand All @@ -49,7 +49,7 @@ 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', {
'headers': {
headers: {
'Content-Type': 'application/json'
}
})
Expand All @@ -76,11 +76,11 @@ 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', {
'headers': {
headers: {
'Content-Type': 'application/json'
},
'body': {
'hello': 'world'
body: {
hello: 'world'
}
})
`)
Expand Down Expand Up @@ -110,7 +110,7 @@ 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?foo=bar&bar=foo', {
'headers': {
headers: {
'Content-Type': 'application/json'
}
})
Expand All @@ -135,7 +135,7 @@ const { statusCode, headers, body } = await request('https://example.com?foo=bar
expect(source.code).toBe(`import { request } from 'undici'
const { statusCode, headers, body } = await request('https://example.com', {
'headers': {
headers: {
'Set-Cookie': 'foo=bar; bar=foo'
}
})
Expand Down
39 changes: 34 additions & 5 deletions packages/snippetz-plugin-undici/src/undici.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,39 @@ function arrayToObject(items: any) {
}, {})
}

export function formatAsJavaScriptObject(data: Record<string, any>) {
return JSON.stringify(data, null, 2)
.replaceAll(`'`, `\'`)
.replaceAll(`"`, `'`)
function isKeyNeedsQuotes(key) {
return /\s|-/.test(key);
}

export function objectToString(obj: Record<string, any>, indent = 0): string {
let parts = [];
let indentation = ' '.repeat(indent);
let innerIndentation = ' '.repeat(indent + 2);

for (const [key, value] of Object.entries(obj)) {
let formattedKey = isKeyNeedsQuotes(key) ? `'${key}'` : key;

if (Array.isArray(value)) {
const arrayString = value.map(item => {
if (typeof item === 'string') {
return `'${item}'`;
} else if (item && typeof item === 'object') {
return objectToString(item, indent + 2);
} else {
return item;
}
}).join(`, ${innerIndentation}`);
parts.push(`${innerIndentation}${formattedKey}: [${arrayString}]`);
} else if (value && typeof value === 'object') {
parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`);
} else if (typeof value === 'string') {
parts.push(`${innerIndentation}${formattedKey}: '${value}'`);
} else {
parts.push(`${innerIndentation}${formattedKey}: ${value}`);
}
}

return `{\n${parts.join(',\n')}\n${indentation}}`;
}

export function undici(request: Partial<Request>): Source {
Expand Down Expand Up @@ -79,7 +108,7 @@ export function undici(request: Partial<Request>): Source {

// Transform to JSON
const jsonOptions = Object.keys(options).length
? `, ${formatAsJavaScriptObject(options)}`
? `, ${objectToString(options)}`
: ''

// Code Template
Expand Down
1 change: 0 additions & 1 deletion packages/snippetz/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './format'
export * from './snippetz'
export * from './types'

0 comments on commit 984b039

Please sign in to comment.