Skip to content

Commit 1d7a971

Browse files
committed
Release 0.1.7
1 parent e867ce1 commit 1d7a971

File tree

189 files changed

+1673
-1421
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+1673
-1421
lines changed

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
{
22
"name": "@fern-api/codecombat",
3-
"version": "0.0.18",
3+
"version": "0.1.7",
44
"private": false,
55
"repository": "https://github.com/codecombat/codecombat-node",
66
"main": "./index.js",
77
"types": "./index.d.ts",
88
"scripts": {
99
"format": "prettier --write 'src/**/*.ts'",
10-
"build": "tsc && tsc-alias",
10+
"build": "tsc",
1111
"prepack": "cp -rv dist/. ."
1212
},
1313
"dependencies": {
14-
"@ungap/url-search-params": "0.2.2",
1514
"url-join": "4.0.1",
1615
"@types/url-join": "4.0.1",
17-
"js-base64": "3.7.2",
18-
"axios": "0.27.2"
16+
"@ungap/url-search-params": "0.2.2",
17+
"axios": "0.27.2",
18+
"js-base64": "3.7.2"
1919
},
2020
"devDependencies": {
2121
"@types/node": "17.0.33",
2222
"prettier": "2.7.1",
23-
"tsc-alias": "1.7.1",
2423
"typescript": "4.6.4"
2524
}
2625
}

src/Client.ts

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import * as environments from "./environments";
66
import * as core from "./core";
7+
import * as CodeCombat from "./api";
8+
import * as serializers from "./serialization";
9+
import urlJoin from "url-join";
10+
import * as errors from "./errors";
711
import { Auth } from "./api/resources/auth/client/Client";
812
import { Clans } from "./api/resources/clans/client/Client";
913
import { Classrooms } from "./api/resources/classrooms/client/Client";
@@ -12,41 +16,111 @@ import { Users } from "./api/resources/users/client/Client";
1216

1317
export declare namespace CodeCombatClient {
1418
interface Options {
15-
environment?: environments.CodeCombatEnvironment | string;
16-
credentials: core.Supplier<core.BasicAuth>;
19+
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
20+
username: core.Supplier<string>;
21+
password: core.Supplier<string>;
22+
}
23+
24+
interface RequestOptions {
25+
timeoutInSeconds?: number;
1726
}
1827
}
1928

2029
export class CodeCombatClient {
21-
constructor(private readonly options: CodeCombatClient.Options) {}
30+
constructor(protected readonly _options: CodeCombatClient.Options) {}
31+
32+
/**
33+
* Adds an OAuth2 identity to the user, so that they can be logged in with that identity. You need to send the OAuth code or the access token to this endpoint. 1. If no access token is provided, it will use your OAuth2 token URL to exchange the given code for an access token. 2. Then it will use the access token (given by you, or received from step 1) to look up the user on your service using the lookup URL, and expects a JSON object in response with an `id` property. 3. It will then save that user `id` to the user in our db as a new OAuthIdentity. In this example, we call your lookup URL (let's say, `https://oauth.provider/user?t=<%= accessToken %>`) with the access token (`1234`). The lookup URL returns `{ id: 'abcd' }` in this case, which we save to the user in our db.
34+
*
35+
*/
36+
public async postUsersHandleOAuthIdentities(
37+
handle: string,
38+
request: CodeCombat.PostUsersHandleOAuthIdentitiesRequest,
39+
requestOptions?: CodeCombatClient.RequestOptions
40+
): Promise<CodeCombat.UserResponse> {
41+
const _response = await core.fetcher({
42+
url: urlJoin(
43+
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
44+
`users/${handle}/o-auth-identities`
45+
),
46+
method: "POST",
47+
headers: {
48+
Authorization: await this._getAuthorizationHeader(),
49+
"X-Fern-Language": "JavaScript",
50+
"X-Fern-SDK-Name": "@fern-api/codecombat",
51+
"X-Fern-SDK-Version": "0.1.7",
52+
},
53+
contentType: "application/json",
54+
body: await serializers.PostUsersHandleOAuthIdentitiesRequest.jsonOrThrow(request, {
55+
unrecognizedObjectKeys: "strip",
56+
}),
57+
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
58+
});
59+
if (_response.ok) {
60+
return await serializers.UserResponse.parseOrThrow(_response.body, {
61+
unrecognizedObjectKeys: "passthrough",
62+
allowUnrecognizedUnionMembers: true,
63+
allowUnrecognizedEnumValues: true,
64+
breadcrumbsPrefix: ["response"],
65+
});
66+
}
67+
68+
if (_response.error.reason === "status-code") {
69+
throw new errors.CodeCombatError({
70+
statusCode: _response.error.statusCode,
71+
body: _response.error.body,
72+
});
73+
}
2274

23-
private _auth: Auth | undefined;
75+
switch (_response.error.reason) {
76+
case "non-json":
77+
throw new errors.CodeCombatError({
78+
statusCode: _response.error.statusCode,
79+
body: _response.error.rawBody,
80+
});
81+
case "timeout":
82+
throw new errors.CodeCombatTimeoutError();
83+
case "unknown":
84+
throw new errors.CodeCombatError({
85+
message: _response.error.errorMessage,
86+
});
87+
}
88+
}
89+
90+
protected _auth: Auth | undefined;
2491

2592
public get auth(): Auth {
26-
return (this._auth ??= new Auth(this.options));
93+
return (this._auth ??= new Auth(this._options));
2794
}
2895

29-
private _clans: Clans | undefined;
96+
protected _clans: Clans | undefined;
3097

3198
public get clans(): Clans {
32-
return (this._clans ??= new Clans(this.options));
99+
return (this._clans ??= new Clans(this._options));
33100
}
34101

35-
private _classrooms: Classrooms | undefined;
102+
protected _classrooms: Classrooms | undefined;
36103

37104
public get classrooms(): Classrooms {
38-
return (this._classrooms ??= new Classrooms(this.options));
105+
return (this._classrooms ??= new Classrooms(this._options));
39106
}
40107

41-
private _stats: Stats | undefined;
108+
protected _stats: Stats | undefined;
42109

43110
public get stats(): Stats {
44-
return (this._stats ??= new Stats(this.options));
111+
return (this._stats ??= new Stats(this._options));
45112
}
46113

47-
private _users: Users | undefined;
114+
protected _users: Users | undefined;
48115

49116
public get users(): Users {
50-
return (this._users ??= new Users(this.options));
117+
return (this._users ??= new Users(this._options));
118+
}
119+
120+
protected async _getAuthorizationHeader() {
121+
return core.BasicAuth.toAuthorizationHeader({
122+
username: await core.Supplier.get(this._options.username),
123+
password: await core.Supplier.get(this._options.password),
124+
});
51125
}
52126
}

src/api/client/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./requests";

src/api/resources/users/client/requests/AddOAuthIdentityRequest.ts renamed to src/api/client/requests/PostUsersHandleOAuthIdentitiesRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
export interface AddOAuthIdentityRequest {
5+
export interface PostUsersHandleOAuthIdentitiesRequest {
66
/** Your OAuth Provider ID. */
77
provider: string;
88
/** Will be passed through your lookup URL to get the user ID. Required if no `code`. */

src/api/client/requests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { PostUsersHandleOAuthIdentitiesRequest } from "./PostUsersHandleOAuthIdentitiesRequest";

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from "./types";
12
export * from "./resources";
3+
export * from "./client";

src/api/resources/auth/client/Client.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,34 @@
44

55
import * as environments from "../../../../environments";
66
import * as core from "../../../../core";
7-
import { CodeCombat } from "@fern-api/codecombat";
8-
import URLSearchParams from "@ungap/url-search-params";
7+
import * as CodeCombat from "../../..";
8+
import { default as URLSearchParams } from "@ungap/url-search-params";
99
import urlJoin from "url-join";
1010
import * as errors from "../../../../errors";
1111

1212
export declare namespace Auth {
1313
interface Options {
14-
environment?: environments.CodeCombatEnvironment | string;
15-
credentials: core.Supplier<core.BasicAuth>;
14+
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
15+
username: core.Supplier<string>;
16+
password: core.Supplier<string>;
17+
}
18+
19+
interface RequestOptions {
20+
timeoutInSeconds?: number;
1621
}
1722
}
1823

1924
export class Auth {
20-
constructor(private readonly options: Auth.Options) {}
25+
constructor(protected readonly _options: Auth.Options) {}
2126

2227
/**
23-
* Logs a [user](#users) in. #### Example ```javascript url = `https://codecombat.com/auth/login-o-auth?provider=${OAUTH_PROVIDER_ID}&accessToken=1234` res.redirect(url) // User is sent to this CodeCombat URL and assuming everything checks out, // is logged in and redirected to the home page. ``` In this example, we call your lookup URL (let's say, `https://oauth.provider/user?t=<%= accessToken %>`) with the access token (`1234`). The lookup URL returns `{ id: 'abcd' }` in this case. We will match this `id` with the OAuthIdentity stored in the user information in our db. If everything checks out, the user is logged in and redirected to the home page.
28+
* Logs a user in. In this example, we call your lookup URL (let's say, `https://oauth.provider/user?t=<%= accessToken %>`) with the access token (`1234`). The lookup URL returns `{ id: 'abcd' }` in this case. We will match this `id` with the OAuthIdentity stored in the user information in our db. If everything checks out, the user is logged in and redirected to the home page.
2429
*
2530
*/
26-
public async get(request: CodeCombat.GetUserAuthRequest): Promise<void> {
31+
public async loginOauth(
32+
request: CodeCombat.LoginOauthRequest,
33+
requestOptions?: Auth.RequestOptions
34+
): Promise<void> {
2735
const { provider, accessToken, code, redirect, errorRedirect } = request;
2836
const _queryParams = new URLSearchParams();
2937
_queryParams.append("provider", provider);
@@ -45,15 +53,19 @@ export class Auth {
4553

4654
const _response = await core.fetcher({
4755
url: urlJoin(
48-
this.options.environment ?? environments.CodeCombatEnvironment.Production,
49-
"/auth/login-o-auth"
56+
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
57+
"auth/login-o-auth"
5058
),
5159
method: "GET",
5260
headers: {
5361
Authorization: await this._getAuthorizationHeader(),
62+
"X-Fern-Language": "JavaScript",
63+
"X-Fern-SDK-Name": "@fern-api/codecombat",
64+
"X-Fern-SDK-Version": "0.1.7",
5465
},
5566
contentType: "application/json",
5667
queryParameters: _queryParams,
68+
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
5769
});
5870
if (_response.ok) {
5971
return;
@@ -81,12 +93,10 @@ export class Auth {
8193
}
8294
}
8395

84-
private async _getAuthorizationHeader() {
85-
const credentials = await core.Supplier.get(this.options.credentials);
86-
if (credentials != null) {
87-
return core.BasicAuth.toAuthorizationHeader(await core.Supplier.get(credentials));
88-
}
89-
90-
return undefined;
96+
protected async _getAuthorizationHeader() {
97+
return core.BasicAuth.toAuthorizationHeader({
98+
username: await core.Supplier.get(this._options.username),
99+
password: await core.Supplier.get(this._options.password),
100+
});
91101
}
92102
}

src/api/resources/auth/client/requests/GetUserAuthRequest.ts renamed to src/api/resources/auth/client/requests/LoginOauthRequest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* This file was auto-generated by Fern from our API Definition.
33
*/
44

5-
export interface GetUserAuthRequest {
5+
export interface LoginOauthRequest {
66
/**
77
* Your OAuth Provider ID
88
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { GetUserAuthRequest } from "./GetUserAuthRequest";
1+
export { LoginOauthRequest } from "./LoginOauthRequest";

src/api/resources/clans/client/Client.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,56 @@
44

55
import * as environments from "../../../../environments";
66
import * as core from "../../../../core";
7-
import { CodeCombat } from "@fern-api/codecombat";
8-
import urlJoin from "url-join";
7+
import * as CodeCombat from "../../..";
98
import * as serializers from "../../../../serialization";
9+
import urlJoin from "url-join";
1010
import * as errors from "../../../../errors";
1111

1212
export declare namespace Clans {
1313
interface Options {
14-
environment?: environments.CodeCombatEnvironment | string;
15-
credentials: core.Supplier<core.BasicAuth>;
14+
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
15+
username: core.Supplier<string>;
16+
password: core.Supplier<string>;
17+
}
18+
19+
interface RequestOptions {
20+
timeoutInSeconds?: number;
1621
}
1722
}
1823

1924
export class Clans {
20-
constructor(private readonly options: Clans.Options) {}
25+
constructor(protected readonly _options: Clans.Options) {}
2126

2227
/**
2328
* Upserts a user into the clan.
2429
*/
25-
public async upsertClan(handle: string, request: CodeCombat.UpsertClanRequest): Promise<CodeCombat.ClanResponse> {
30+
public async upsertMember(
31+
handle: string,
32+
request: CodeCombat.ClansUpsertMemberRequest,
33+
requestOptions?: Clans.RequestOptions
34+
): Promise<CodeCombat.ClanResponse> {
2635
const _response = await core.fetcher({
2736
url: urlJoin(
28-
this.options.environment ?? environments.CodeCombatEnvironment.Production,
29-
`/clan/${handle}/members`
37+
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
38+
`clan/${handle}/members`
3039
),
3140
method: "PUT",
3241
headers: {
3342
Authorization: await this._getAuthorizationHeader(),
43+
"X-Fern-Language": "JavaScript",
44+
"X-Fern-SDK-Name": "@fern-api/codecombat",
45+
"X-Fern-SDK-Version": "0.1.7",
3446
},
3547
contentType: "application/json",
36-
body: await serializers.UpsertClanRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
48+
body: await serializers.ClansUpsertMemberRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
49+
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
3750
});
3851
if (_response.ok) {
3952
return await serializers.ClanResponse.parseOrThrow(_response.body, {
4053
unrecognizedObjectKeys: "passthrough",
4154
allowUnrecognizedUnionMembers: true,
4255
allowUnrecognizedEnumValues: true,
56+
breadcrumbsPrefix: ["response"],
4357
});
4458
}
4559

@@ -65,12 +79,10 @@ export class Clans {
6579
}
6680
}
6781

68-
private async _getAuthorizationHeader() {
69-
const credentials = await core.Supplier.get(this.options.credentials);
70-
if (credentials != null) {
71-
return core.BasicAuth.toAuthorizationHeader(await core.Supplier.get(credentials));
72-
}
73-
74-
return undefined;
82+
protected async _getAuthorizationHeader() {
83+
return core.BasicAuth.toAuthorizationHeader({
84+
username: await core.Supplier.get(this._options.username),
85+
password: await core.Supplier.get(this._options.password),
86+
});
7587
}
7688
}

0 commit comments

Comments
 (0)