Skip to content

Commit bf4b9bc

Browse files
committed
Add legacy client integration test and README.md section
1 parent e46be2d commit bf4b9bc

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,69 @@ await client.payments.create({
5353
});
5454
```
5555

56+
## Legacy SDK
57+
58+
While the new SDK has a lot of improvements, we at Square understand that it takes time to upgrade when there are breaking changes.
59+
To make the migration easier, the new SDK also exports the legacy SDK as `square/legacy`. Here's an example of how you can use the
60+
legacy SDK alongside the new SDK inside a single file:
61+
62+
```typescript
63+
import { randomUUID } from "crypto";
64+
import { Square, SquareClient } from "square";
65+
import { Client } from "square/legacy";
66+
67+
const client = new SquareClient({
68+
token: process.env.SQUARE_ACCESS_TOKEN,
69+
});
70+
71+
const legacyClient = new Client({
72+
bearerAuthCredentials: {
73+
accessToken: process.env.SQUARE_ACCESS_TOKEN!,
74+
},
75+
});
76+
77+
async function getLocation(): Promise<Square.Location> {
78+
return (
79+
await client.locations.get({
80+
locationId: "YOUR_LOCATION_ID",
81+
})
82+
).location!;
83+
}
84+
85+
async function createOrder() {
86+
const location = await getLocation();
87+
await legacyClient.ordersApi.createOrder({
88+
idempotencyKey: randomUUID(),
89+
order: {
90+
locationId: location.id!,
91+
lineItems: [
92+
{
93+
name: "New Item",
94+
quantity: "1",
95+
basePriceMoney: {
96+
amount: BigInt(100),
97+
currency: "USD",
98+
},
99+
},
100+
],
101+
},
102+
});
103+
}
104+
105+
createOrder();
106+
```
107+
108+
We recommend migrating to the new SDK using the following steps:
109+
110+
1. Upgrade the NPM module to `^40.0.0`
111+
2. Search and replace all requires and imports from `"square"` to `"square/legacy"`
112+
113+
- For required, replace `require("square")` with `require("square/legacy")`
114+
- For imports, replace `from "square"` with `from "square/legacy"`
115+
- For dynamic imports, replace `import("square")` with `import("square/legacy")`
116+
117+
3. Gradually move over to use the new SDK by importing it from the `"square"` import.
118+
56119
## Request And Response Types
57120

58121
The SDK exports all request and response types as TypeScript interfaces. Simply import them with the

tests/integration/helpers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { randomUUID } from "crypto";
22
import { readFile } from "fs/promises";
33
import path from "path";
4+
import { Client, Environment } from "../../legacy/exports";
45
import { Square, SquareClient, SquareEnvironment } from "../../src";
56

67
export const createClient = (): SquareClient => {
@@ -14,6 +15,19 @@ export const createClient = (): SquareClient => {
1415
});
1516
};
1617

18+
export const createLegacyClient = (): Client => {
19+
const token = process.env.TEST_SQUARE_TOKEN;
20+
if (!token) {
21+
throw new Error("TEST_SQUARE_TOKEN is not set");
22+
}
23+
return new Client({
24+
environment: Environment.Sandbox,
25+
bearerAuthCredentials: {
26+
accessToken: token,
27+
},
28+
});
29+
};
30+
1731
export function newTestUuid(): string {
1832
return randomUUID();
1933
}

tests/integration/legacy.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Client } from "../../legacy/exports";
2+
import { createLegacyClient } from "./helpers";
3+
4+
describe("Legacy SDK", () => {
5+
const legacyClient: Client = createLegacyClient();
6+
7+
it("should list locations", async () => {
8+
const response = await legacyClient.locationsApi.listLocations();
9+
10+
expect(response.result.locations).toBeDefined();
11+
expect(response.result.locations?.length).toBeGreaterThan(0);
12+
});
13+
});

0 commit comments

Comments
 (0)