diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 4b1304f..6ea791f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -43,3 +43,25 @@ jobs: run: pnpm install - name: Lint JS files run: pnpm run lint + Typecheck: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: "20" + - name: Restore node_modules cache + uses: actions/cache@v4 + with: + path: node_modules + key: ${{ runner.os }}-node-modules-${{ hashFiles('package.json') }} + restore-keys: | + ${{ runner.os }}-node-modules + - name: Install pnpm + uses: pnpm/action-setup@v4 + - name: Install dependencies + run: pnpm install + - name: Typecheck files + run: pnpm run typecheck diff --git a/package.json b/package.json index baf0439..dbea021 100644 --- a/package.json +++ b/package.json @@ -23,17 +23,15 @@ "files": [ "dist" ], - "dependencies": { - "@ungap/structured-clone": "^1.0.1" - }, "devDependencies": { "prettier": "^3.3.3", "vite": "^6.3.5", "vitest": "^3.2.4" }, "scripts": { - "build": "vite build", - "lint": "prettier --check 'src/**/*.js'", + "typecheck": "tsc --noEmit", + "build": "vite build && tsc", + "lint": "prettier --check 'src/**/*'", "test": "vitest" }, "packageManager": "pnpm@10.12.1" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b3b91ab..529d5f2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,10 +7,6 @@ settings: importers: .: - dependencies: - '@ungap/structured-clone': - specifier: ^1.0.1 - version: 1.3.0 devDependencies: prettier: specifier: ^3.3.3 @@ -292,9 +288,6 @@ packages: '@types/node@24.0.3': resolution: {integrity: sha512-R4I/kzCYAdRLzfiCabn9hxWfbuHS573x+r0dJMkkzThEa7pbrcDWK+9zu3e7aBOouf+rQAciqPFMnxwr0aWgKg==} - '@ungap/structured-clone@1.3.0': - resolution: {integrity: sha512-WmoN8qaIAo7WTYWbAZuG8PYEhn5fkz7dZrqTBZ7dtt//lL2Gwms1IcnQ5yHqjDfX8Ft5j4YzDM23f87zBfDe9g==} - '@vitest/expect@3.2.4': resolution: {integrity: sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==} @@ -699,8 +692,6 @@ snapshots: undici-types: 7.8.0 optional: true - '@ungap/structured-clone@1.3.0': {} - '@vitest/expect@3.2.4': dependencies: '@types/chai': 5.2.2 diff --git a/src/__tests__/alchemyApiDeserializer.spec.js b/src/__tests__/alchemyApiDeserializer.spec.ts similarity index 99% rename from src/__tests__/alchemyApiDeserializer.spec.js rename to src/__tests__/alchemyApiDeserializer.spec.ts index 7674dd7..f922a62 100644 --- a/src/__tests__/alchemyApiDeserializer.spec.js +++ b/src/__tests__/alchemyApiDeserializer.spec.ts @@ -1,4 +1,5 @@ import { deserializePage, deserializePages } from "../alchemyApiDeserializer" +import { describe, expect, it } from "vitest" describe("deserializePage", () => { it("does not return any deprecated elements for single page", () => { diff --git a/src/__tests__/deserialize.spec.js b/src/__tests__/deserialize.spec.ts similarity index 95% rename from src/__tests__/deserialize.spec.js rename to src/__tests__/deserialize.spec.ts index cb8c3fc..d36c8a1 100644 --- a/src/__tests__/deserialize.spec.js +++ b/src/__tests__/deserialize.spec.ts @@ -1,4 +1,5 @@ import { deserialize } from "../deserialize" +import { describe, expect, it } from "vitest" describe("deserialize", () => { it("Complex serialize", () => { diff --git a/src/alchemyApiDeserializer.js b/src/alchemyApiDeserializer.ts similarity index 74% rename from src/alchemyApiDeserializer.js rename to src/alchemyApiDeserializer.ts index f87db13..c70d505 100644 --- a/src/alchemyApiDeserializer.js +++ b/src/alchemyApiDeserializer.ts @@ -1,10 +1,10 @@ import { deserialize } from "./deserialize" // Recursively filters all deprecated elements and essences from collection -function filterDeprecatedElements(elements) { - const els = [] +function filterDeprecatedElements(elements: any) { + const els: any = [] - elements.forEach((element) => { + elements.forEach((element: any) => { if (element.nested_elements?.length > 0) { element.nested_elements = filterDeprecatedElements( element.nested_elements @@ -14,7 +14,7 @@ function filterDeprecatedElements(elements) { element.nestedElements = filterDeprecatedElements(element.nestedElements) } if (element.essences?.length > 0) { - element.essences = element.essences.filter((essence) => { + element.essences = element.essences.filter((essence: any) => { return !essence.deprecated }) } @@ -27,16 +27,16 @@ function filterDeprecatedElements(elements) { } // Returns deserialized page without deprecated content -export function deserializePage(pageData) { +export function deserializePage(pageData: any) { const page = deserialize(pageData) page.elements = filterDeprecatedElements(page.elements) return page } // Returns deserialized pages without deprecated content -export function deserializePages(pagesData) { +export function deserializePages(pagesData: any) { const pages = deserialize(pagesData) - pages.forEach((page) => { + pages.forEach((page: any) => { page.elements = filterDeprecatedElements(page.elements) }) return pages diff --git a/src/deserialize.js b/src/deserialize.ts similarity index 80% rename from src/deserialize.js rename to src/deserialize.ts index a1edacc..20f6d2b 100644 --- a/src/deserialize.js +++ b/src/deserialize.ts @@ -1,6 +1,4 @@ -import structuredClone from "@ungap/structured-clone" - -export function deserialize(originalResponse, options = {}) { +export function deserialize(originalResponse: any, options = {}) { const response = structuredClone(originalResponse) if (!options) { options = {} @@ -9,7 +7,7 @@ export function deserialize(originalResponse, options = {}) { const included = response.included || [] if (Array.isArray(response.data)) { - return response.data.map((data) => { + return response.data.map((data: any) => { return parseJsonApiSimpleResourceData(data, included, false, options) }) } else { @@ -22,7 +20,12 @@ export function deserialize(originalResponse, options = {}) { } } -function parseJsonApiSimpleResourceData(data, included, useCache, options) { +function parseJsonApiSimpleResourceData( + data: any, + included: any, + useCache: any, + options: any +) { if (!included.cached) { included.cached = {} } @@ -47,9 +50,9 @@ function parseJsonApiSimpleResourceData(data, included, useCache, options) { const relationRef = data.relationships[relationName] if (Array.isArray(relationRef.data)) { - const items = [] + const items: any = [] - relationRef.data.forEach((relationData) => { + relationRef.data.forEach((relationData: any) => { const item = findJsonApiIncluded( included, relationData.type, @@ -77,10 +80,10 @@ function parseJsonApiSimpleResourceData(data, included, useCache, options) { return resource } -function findJsonApiIncluded(included, type, id, options) { +function findJsonApiIncluded(included: any, type: any, id: any, options: any) { let found = null - included.forEach((item) => { + included.forEach((item: any) => { if (item.type === type && item.id === id) { found = parseJsonApiSimpleResourceData(item, included, true, options) } diff --git a/src/main.js b/src/main.js deleted file mode 100644 index 3ed4a63..0000000 --- a/src/main.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./alchemyApiDeserializer.js" -export * from "./deserialize.js" diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..4833862 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,2 @@ +export * from "./alchemyApiDeserializer" +export * from "./deserialize" diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..bc4b739 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": "dist", + "removeComments": true, + "noImplicitAny": true, + "declaration": true, + "emitDeclarationOnly": true + }, + "include": ["src/**/*"], + "exclude": ["src/__tests__"] +} diff --git a/vite.config.ts b/vite.config.ts index d678d95..10e9ee2 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,8 +1,6 @@ import { defineConfig } from "vite" import { resolve } from "path" -const external = ["@ungap/structured-clone"] - export default defineConfig({ build: { lib: { @@ -12,9 +10,6 @@ export default defineConfig({ }, formats: ["es"], fileName: (_format, entryName) => `${entryName}.js` - }, - rollupOptions: { - external } } }) diff --git a/vitest.config.mjs b/vitest.config.mjs index ad3b8df..90f9f26 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -2,6 +2,6 @@ import { defineConfig } from "vitest/config" export default defineConfig({ test: { - globals: true + globals: false } })