Skip to content

Commit b759d24

Browse files
committed
refactor: to typescript
We change the source files to .ts files and type everything with any to initially satisfy the typescript compiler.
1 parent 4d1c75a commit b759d24

File tree

8 files changed

+35
-16
lines changed

8 files changed

+35
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"vitest": "^3.2.4"
3030
},
3131
"scripts": {
32-
"build": "vite build",
32+
"build": "vite build && tsc",
3333
"lint": "prettier --check 'src/**/*.js'",
3434
"test": "vitest"
3535
},

src/__tests__/alchemyApiDeserializer.spec.js renamed to src/__tests__/alchemyApiDeserializer.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { deserializePage, deserializePages } from "../alchemyApiDeserializer"
2+
import { describe, expect, it } from "vitest"
23

34
describe("deserializePage", () => {
45
it("does not return any deprecated elements for single page", () => {

src/__tests__/deserialize.spec.js renamed to src/__tests__/deserialize.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { deserialize } from "../deserialize"
2+
import { describe, expect, it } from "vitest"
23

34
describe("deserialize", () => {
45
it("Complex serialize", () => {

src/alchemyApiDeserializer.js renamed to src/alchemyApiDeserializer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { deserialize } from "./deserialize"
22

33
// Recursively filters all deprecated elements and essences from collection
4-
function filterDeprecatedElements(elements) {
5-
const els = []
4+
function filterDeprecatedElements(elements: any) {
5+
const els: any = []
66

7-
elements.forEach((element) => {
7+
elements.forEach((element: any) => {
88
if (element.nested_elements?.length > 0) {
99
element.nested_elements = filterDeprecatedElements(
1010
element.nested_elements
@@ -14,7 +14,7 @@ function filterDeprecatedElements(elements) {
1414
element.nestedElements = filterDeprecatedElements(element.nestedElements)
1515
}
1616
if (element.essences?.length > 0) {
17-
element.essences = element.essences.filter((essence) => {
17+
element.essences = element.essences.filter((essence: any) => {
1818
return !essence.deprecated
1919
})
2020
}
@@ -27,16 +27,16 @@ function filterDeprecatedElements(elements) {
2727
}
2828

2929
// Returns deserialized page without deprecated content
30-
export function deserializePage(pageData) {
30+
export function deserializePage(pageData: any) {
3131
const page = deserialize(pageData)
3232
page.elements = filterDeprecatedElements(page.elements)
3333
return page
3434
}
3535

3636
// Returns deserialized pages without deprecated content
37-
export function deserializePages(pagesData) {
37+
export function deserializePages(pagesData: any) {
3838
const pages = deserialize(pagesData)
39-
pages.forEach((page) => {
39+
pages.forEach((page: any) => {
4040
page.elements = filterDeprecatedElements(page.elements)
4141
})
4242
return pages

src/deserialize.js renamed to src/deserialize.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function deserialize(originalResponse, options = {}) {
1+
export function deserialize(originalResponse: any, options = {}) {
22
const response = structuredClone(originalResponse)
33
if (!options) {
44
options = {}
@@ -7,7 +7,7 @@ export function deserialize(originalResponse, options = {}) {
77
const included = response.included || []
88

99
if (Array.isArray(response.data)) {
10-
return response.data.map((data) => {
10+
return response.data.map((data: any) => {
1111
return parseJsonApiSimpleResourceData(data, included, false, options)
1212
})
1313
} else {
@@ -20,7 +20,12 @@ export function deserialize(originalResponse, options = {}) {
2020
}
2121
}
2222

23-
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
23+
function parseJsonApiSimpleResourceData(
24+
data: any,
25+
included: any,
26+
useCache: any,
27+
options: any
28+
) {
2429
if (!included.cached) {
2530
included.cached = {}
2631
}
@@ -45,9 +50,9 @@ function parseJsonApiSimpleResourceData(data, included, useCache, options) {
4550
const relationRef = data.relationships[relationName]
4651

4752
if (Array.isArray(relationRef.data)) {
48-
const items = []
53+
const items: any = []
4954

50-
relationRef.data.forEach((relationData) => {
55+
relationRef.data.forEach((relationData: any) => {
5156
const item = findJsonApiIncluded(
5257
included,
5358
relationData.type,
@@ -75,10 +80,10 @@ function parseJsonApiSimpleResourceData(data, included, useCache, options) {
7580
return resource
7681
}
7782

78-
function findJsonApiIncluded(included, type, id, options) {
83+
function findJsonApiIncluded(included: any, type: any, id: any, options: any) {
7984
let found = null
8085

81-
included.forEach((item) => {
86+
included.forEach((item: any) => {
8287
if (item.type === type && item.id === id) {
8388
found = parseJsonApiSimpleResourceData(item, included, true, options)
8489
}
File renamed without changes.

tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"outDir": "dist",
5+
"removeComments": true,
6+
"noImplicitAny": true,
7+
"declaration": true,
8+
"emitDeclarationOnly": true
9+
},
10+
"include": ["src/**/*"],
11+
"exclude": ["src/__tests__"]
12+
}

vitest.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { defineConfig } from "vitest/config"
22

33
export default defineConfig({
44
test: {
5-
globals: true
5+
globals: false
66
}
77
})

0 commit comments

Comments
 (0)