Skip to content

Commit

Permalink
chore: Migrate to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Nov 25, 2022
1 parent e18b59d commit e73067c
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 225 deletions.
9 changes: 0 additions & 9 deletions packages/medusa-core-utils/.babelrc

This file was deleted.

13 changes: 13 additions & 0 deletions packages/medusa-core-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
globals: {
"ts-jest": {
tsConfig: "tsconfig.spec.json",
isolatedModules: false,
},
},
transform: {
"^.+\\.[jt]s?$": "ts-jest",
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `jsx`, `ts`, `tsx`, `json`],
}
11 changes: 2 additions & 9 deletions packages/medusa-core-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@
"test": "jest",
"build": "tsc --build",
"prepare": "cross-env NODE_ENV=production yarn run build",
"watch": "babel -w src --out-dir dist/ --ignore **/__tests__"
"watch": "tsc --watch"
},
"author": "Sebastian Rindom",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.13.0",
"@babel/core": "^7.13.8",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-transform-classes": "^7.9.5",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.13.9",
"@babel/preset-typescript": "^7.16.0",
"@babel/runtime": "^7.13.9",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.1",
"cross-env": "^5.2.1",
"jest": "^25.5.2",
"ts-jest": "^25.5.1",
"typescript": "^4.4.4"
},
"dependencies": {
Expand Down
17 changes: 0 additions & 17 deletions packages/medusa-core-utils/src/compare-objects.js

This file was deleted.

21 changes: 21 additions & 0 deletions packages/medusa-core-utils/src/compare-objects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { differenceWith, isEqual } from "lodash"

type InputObj = Object | Record<string | number | symbol, unknown>

function compareObjectsByProp(
object1: InputObj,
object2: InputObj,
prop: string
): boolean {
if (Array.isArray(object1[prop])) {
object2[prop] = object2[prop].map(({ _id, ...rest }) => rest)
return differenceWith(object1[prop], object2[prop], isEqual).length === 0
} else if (typeof object1[prop] === "object") {
delete object2[prop]._id
return isEqual(object1[prop], object2[prop])
} else {
return object1[prop] === object2[prop]
}
}

export default compareObjectsByProp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import zeroDecimalCurrencies from "./zero-decimal-currencies"

const computerizeAmount = (amount, currency) => {
const computerizeAmount = (amount: number, currency: string): number => {
let divisor = 100

if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const countries = [
type Country = {
alpha2: string
name: string
alpha3: string
numeric: string
}

export const countries: Country[] = [
{ alpha2: "AF", name: "Afghanistan", alpha3: "AFG", numeric: "004" },
{ alpha2: "AL", name: "Albania", alpha3: "ALB", numeric: "008" },
{ alpha2: "DZ", name: "Algeria", alpha3: "DZA", numeric: "012" },
Expand Down Expand Up @@ -402,27 +409,17 @@ export const countries = [
{ alpha2: "AX", name: "Åland Islands", alpha3: "ALA", numeric: "248" },
]

export function isoCountryLookup(country) {
export function isoCountryLookup(country: string): string {
const normalizedCountry = country.toUpperCase()

let isoRecord = countries.find(record => {
return record.name === normalizedCountry
let isoRecord = countries.find((record) => {
return (
record.name.toUpperCase() === normalizedCountry ||
record.alpha2.toUpperCase() === normalizedCountry ||
record.alpha3.toUpperCase() === normalizedCountry
)
})

// Try alpha2 instead
if (!isoRecord) {
isoRecord = countries.find(record => {
return record.alpha2 === normalizedCountry
})
}

// Try alpha3
if (!isoRecord) {
isoRecord = countries.find(record => {
return record.alpha3 === normalizedCountry
})
}

if (!isoRecord) {
throw new Error("Invalid country name")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Module from "module"
const Module = require("module")
import path from "path"

const fallback = filename => {
const fallback = (filename: string) => {
const mod = new Module(filename)

mod.filename = filename
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import path from "path"
import { join } from "path"

/**
* Attempts to resolve the config file in a given root directory.
* @param {string} rootDir - the directory to find the config file in.
* @param {string} configName - the name of the config file.
* @return {object} an object containing the config module and its path as well as an error property if the config couldn't be loaded.
*/
function getConfigFile(rootDir, configName) {
const configPath = path.join(rootDir, configName)
function getConfigFile<TConfig = unknown>(
rootDir: string,
configName: string
): { configModule: TConfig; configFilePath: string } | { error: any } {
const configPath = join(rootDir, configName)
let configFilePath = ``
let configModule

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path"

export const getMedusaVersion = () => {
export const getMedusaVersion = (): string => {
try {
return require(path.join(
process.cwd(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import zeroDecimalCurrencies from "./zero-decimal-currencies"

const humanizeAmount = (amount, currency) => {
const humanizeAmount = (amount: number, currency: string): number => {
let divisor = 100

if (zeroDecimalCurrencies.includes(currency.toLowerCase())) {
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions packages/medusa-core-utils/src/transform-idable-fields.js

This file was deleted.

50 changes: 50 additions & 0 deletions packages/medusa-core-utils/src/transform-idable-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
type ComputePropertyNames<
T,
TKey extends keyof T & string,
TFields extends (keyof T | string)[] = (keyof T | string)[]
> = TKey extends TFields[number]
? T[TKey] extends string
? `${TKey}_id`
: TKey
: TKey

/**
* Takes an object and a list of fields to tranform in that object. If the field
* exists on the object and its value is a string it will append `_id` to the
* field name. This is used when allowing API calls to hold either ID or object
* values in the payload. The method returns a new object with the
* transformation.
* @param obj - the object to transform
* @param fields - the fields to apply transformation to
* @returns the transformed object
*
* @example
* ```ts
* const obj = { test: "test", toto: 1, tata: Symbol("tata") }
* const out = transformIdableFields(obj, ["test"]) // out: { test_id: string, toto: number, tata: symbol }
* ```
*/
export const transformIdableFields = <
T = Record<string, unknown>,
TFields extends (keyof T | string)[] = (keyof T | string)[],
TOutput = {
[P in ComputePropertyNames<T, keyof T & string, TFields>]: P extends keyof T
? T[P]
: string
}
>(
obj: T,
fields: TFields
): TOutput => {
const ret = { ...obj }

for (let key of fields) {
key = key as string
if (key in obj && typeof obj[key] === "string") {
ret[`${key.toString()}_id`] = obj[key]
delete ret[key]
}
}

return ret as unknown as TOutput
}
129 changes: 0 additions & 129 deletions packages/medusa-core-utils/src/validator.js

This file was deleted.

Loading

0 comments on commit e73067c

Please sign in to comment.