Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add t3-env to AvaTax #1636

Merged
merged 10 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-pans-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": minor
---

Adds t3-env library for validating env variables.
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
TEST_SALEOR_API_URL: "op://Shop-ex/saleor-app-avatax-e2e-${{ env.SALEOR_VERSION }}/TEST_SALEOR_API_URL"
E2E_USER_NAME: "op://Shop-ex/saleor-app-avatax-e2e-${{ env.SALEOR_VERSION }}/E2E_USER_NAME"
E2E_USER_PASSWORD: "op://Shop-ex/saleor-app-avatax-e2e-${{ env.SALEOR_VERSION }}/E2E_USER_PASSWORD"
SECRET_KEY: "op://Shop-ex/saleor-app-avatax-e2e-${{ env.SALEOR_VERSION }}/SECRET_KEY"
- name: Generate GraphQL files
run: pnpm --filter=app-avatax generate
- name: Run e2e tests
Expand Down
3 changes: 2 additions & 1 deletion apps/avatax/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
root: true,
extends: ["saleor"],
plugins: ["neverthrow"],
plugins: ["neverthrow", "node"],
rules: {
"turbo/no-undeclared-env-vars": ["error"],
"node/no-process-env": ["error"],
},
parserOptions: {
project: "tsconfig.json",
Expand Down
11 changes: 6 additions & 5 deletions apps/avatax/e2e/setup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import "./data/functions";
import "dotenv/config";
import "./data/functions";

import { request, settings, stash } from "pactum";
import { beforeAll } from "vitest";

import { env } from "../src/env";

beforeAll(() => {
const saleorApiUrl = process.env.TEST_SALEOR_API_URL;
const saleorApiUrl = env.TEST_SALEOR_API_URL;

const staffCredentials = {
email: process.env.E2E_USER_NAME,
password: process.env.E2E_USER_PASSWORD,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

if (!saleorApiUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
CompleteCheckout,
CreateCheckout,
} from "../generated/graphql";
import { getCompleteMoney, getMoney } from "../utils/moneyUtils";
import { getCompleteMoney } from "../utils/moneyUtils";

// Testmo: https://saleor.testmo.net/repositories/6?case_id=16233
describe("App should calculate taxes for checkout with product without tax class TC: AVATAX_1", () => {
Expand Down
5 changes: 3 additions & 2 deletions apps/avatax/e2e/tests/draft_order_basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -16,8 +17,8 @@ import { getCompleteMoney } from "../utils/moneyUtils";
describe("App should calculate taxes for draft order with product with tax class TC: AVATAX_18", () => {
const testCase = e2e("Product with tax class [pricesEnteredWithTax: True]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
6 changes: 3 additions & 3 deletions apps/avatax/e2e/tests/draft_order_catalog_promotion.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -18,8 +18,8 @@ describe("App should calculate taxes on draft order with products on catalog pro
"Draft order with products on catalog promotion [pricesEnteredWithTax: True]",
);
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
6 changes: 3 additions & 3 deletions apps/avatax/e2e/tests/draft_order_changing_lines.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -17,8 +17,8 @@ import { getCompleteMoney } from "../utils/moneyUtils";
describe("App should calculate taxes on draft order when changing lines TC: AVATAX_26", () => {
const testCase = e2e("Changing lines on draft order [pricesEnteredWithTax: True]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -17,8 +17,8 @@ import { getCompleteMoney } from "../utils/moneyUtils";
describe("App should calculate taxes on draft order with entire order voucher applied TC: AVATAX_28", () => {
const testCase = e2e("Draft order with voucher entire order [pricesEnteredWithTax: False]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
6 changes: 3 additions & 3 deletions apps/avatax/e2e/tests/draft_order_with_entity_code.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
DraftOrderComplete,
Expand All @@ -15,8 +15,8 @@ import { getCompleteMoney } from "../utils/moneyUtils";
describe("App should exempt taxes on draft order with metadata avataxEntityCode TC: AVATAX_36", () => {
const testCase = e2e("draft order with avataxEntityCode [pricesEnteredWithTax: True]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME as string,
password: env.E2E_USER_PASSWORD as string,
};

const metadata = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -17,8 +17,8 @@ import { getCompleteMoney } from "../utils/moneyUtils";
describe("App should calculate taxes for draft order with manual total discount applied TC: AVATAX_22", () => {
const testCase = e2e("Product with tax class [pricesEnteredWithTax: True]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import { e2e } from "pactum";
import { describe, it } from "vitest";

import { env } from "../../src/env";
import {
CreateDraftOrder,
CreateOrderLines,
Expand All @@ -17,8 +17,8 @@ import { getCompleteMoney, getMoney } from "../utils/moneyUtils";
describe("App should calculate taxes on draft order when order promotion is applied TC: AVATAX_32", () => {
const testCase = e2e("Draft order with order promotion, [pricesEnteredWithTax: False]");
const staffCredentials = {
email: process.env.E2E_USER_NAME as string,
password: process.env.E2E_USER_PASSWORD as string,
email: env.E2E_USER_NAME,
password: env.E2E_USER_PASSWORD,
};

const CURRENCY = "USD";
Expand Down
1 change: 1 addition & 0 deletions apps/avatax/next.config.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we import t3 env here, so it validates during build?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can but we will have to install additional pkg: https://env.t3.gg/docs/nextjs#validate-schema-on-build-(recommended) as Next.js don't support importing ts files in config natively

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, next 15 does so it can wait

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable node/no-process-env */
// @ts-check

import withBundleAnalyzerConfig from "@next/bundle-analyzer";
Expand Down
2 changes: 2 additions & 0 deletions apps/avatax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@saleor/webhook-utils": "workspace:*",
"@sentry/cli": "../../node_modules/@sentry/cli",
"@sentry/nextjs": "../../node_modules/@sentry/nextjs",
"@t3-oss/env-nextjs": "0.11.1",
"@trpc/client": "10.43.1",
"@trpc/next": "10.43.1",
"@trpc/server": "10.43.1",
Expand Down Expand Up @@ -95,6 +96,7 @@
"eslint": "../../node_modules/eslint",
"eslint-config-saleor": "workspace:*",
"eslint-plugin-neverthrow": "^1.1.4",
"eslint-plugin-node": "11.1.0",
"graphql-config": "5.0.3",
"pactum": "^3.6.0",
"pactum-matchers": "^1.1.6",
Expand Down
22 changes: 11 additions & 11 deletions apps/avatax/saleor-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { APL, FileAPL, SaleorCloudAPL, UpstashAPL } from "@saleor/app-sdk/APL";
import { APL, FileAPL, SaleorCloudAPL } from "@saleor/app-sdk/APL";
import { SaleorApp } from "@saleor/app-sdk/saleor-app";

import { env } from "@/env";

/**
* By default auth data are stored in the `.auth-data.json` (FileAPL).
* For multi-tenant applications and deployments please use UpstashAPL.
Expand All @@ -10,27 +12,25 @@ import { SaleorApp } from "@saleor/app-sdk/saleor-app";
*/

export let apl: APL;
switch (process.env.APL) {
case "upstash":
// Require `UPSTASH_URL` and `UPSTASH_TOKEN` environment variables
apl = new UpstashAPL();
break;
switch (env.APL) {
case "saleor-cloud": {
if (!process.env.REST_APL_ENDPOINT || !process.env.REST_APL_TOKEN) {
if (!env.REST_APL_ENDPOINT || !env.REST_APL_TOKEN) {
throw new Error("Rest APL is not configured - missing env variables. Check saleor-app.ts");
}

apl = new SaleorCloudAPL({
resourceUrl: process.env.REST_APL_ENDPOINT,
token: process.env.REST_APL_TOKEN,
resourceUrl: env.REST_APL_ENDPOINT,
token: env.REST_APL_TOKEN,
});

break;
}
default:
default: {
apl = new FileAPL({
fileName: process.env.FILE_APL_PATH,
fileName: env.FILE_APL_PATH,
});
break;
}
}

export const saleorApp = new SaleorApp({
Expand Down
4 changes: 3 additions & 1 deletion apps/avatax/scripts/migration-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { attachLoggerConsoleTransport, rootLogger } from "@saleor/apps-logger";
import { attachLoggerOtelTransport } from "@saleor/apps-logger/node";

import { env } from "@/env";

import packageJson from "../package.json";
import { loggerContext } from "../src/logger-context";

rootLogger.settings.maskValuesOfKeys = ["username", "password", "token"];

if (process.env.ENABLE_MIGRATION_CONSOLE_LOGGER === "true") {
if (env.ENABLE_MIGRATION_CONSOLE_LOGGER) {
attachLoggerConsoleTransport(rootLogger);
}

Expand Down
18 changes: 9 additions & 9 deletions apps/avatax/scripts/run-webhooks-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ import { otelSdk } from "@saleor/apps-otel/src/instrumentation";
import { ObservabilityAttributes } from "@saleor/apps-otel/src/lib/observability-attributes";
import { WebhookMigrationRunner } from "@saleor/webhook-utils";

import { env } from "@/env";

import { createInstrumentedGraphqlClient } from "../src/lib/create-instrumented-graphql-client";
import { loggerContext } from "../src/logger-context";
import { appWebhooks } from "../webhooks";
import { createMigrationScriptLogger } from "./migration-logger";

const requiredEnvs = ["REST_APL_TOKEN", "REST_APL_ENDPOINT"];

const args = process.argv.slice(2);
const dryRun = args.includes("--dry-run");

if (process.env.OTEL_ENABLED === "true" && process.env.OTEL_SERVICE_NAME) {
if (env.OTEL_ENABLED && env.OTEL_SERVICE_NAME) {
otelSdk.start();
}

const logger = createMigrationScriptLogger("RunWebhooksMigration");

const runMigrations = async () => {
if (!requiredEnvs.every((env) => process.env[env])) {
logger.error(`Missing environment variables: ${requiredEnvs.join(" | ")}`, { dryRun });
logger.info(`Starting webhooks migration`, { dryRun });

if (!env.REST_APL_TOKEN || !env.REST_APL_ENDPOINT) {
logger.error("REST_APL_TOKEN and REST_APL_ENDPOINT must be set", { dryRun });
process.exit(1);
}

logger.info(`Starting webhooks migration`, { dryRun });

const saleorAPL = new SaleorCloudAPL({
token: process.env.REST_APL_TOKEN!,
resourceUrl: process.env.REST_APL_ENDPOINT!,
token: env.REST_APL_TOKEN,
resourceUrl: env.REST_APL_ENDPOINT,
});

logger.info("Fetching environments from the saleor-cloud APL", { dryRun });
Expand Down
6 changes: 4 additions & 2 deletions apps/avatax/sentry.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import * as Sentry from "@sentry/nextjs";

import { env } from "@/env";

Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
dsn: env.NEXT_PUBLIC_SENTRY_DSN,
enableTracing: false,
environment: process.env.ENV,
environment: env.ENV,
includeLocalVariables: true,
ignoreErrors: ["TRPCClientError"],
integrations: [],
Expand Down
6 changes: 4 additions & 2 deletions apps/avatax/sentry.edge.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as Sentry from "@sentry/nextjs";

import { env } from "@/env";

Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
dsn: env.NEXT_PUBLIC_SENTRY_DSN,
enableTracing: false,
environment: process.env.ENV,
environment: env.ENV,
includeLocalVariables: true,
integrations: [
Sentry.localVariablesIntegration({
Expand Down
6 changes: 4 additions & 2 deletions apps/avatax/sentry.server.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as Sentry from "@sentry/nextjs";

import { env } from "@/env";

Sentry.init({
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
dsn: env.NEXT_PUBLIC_SENTRY_DSN,
enableTracing: false,
environment: process.env.ENV,
environment: env.ENV,
includeLocalVariables: true,
integrations: [
Sentry.localVariablesIntegration({
Expand Down
Loading
Loading