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

🌿 Fern Regeneration -- August 29, 2023 #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
{
"name": "@fern-api/codecombat",
"version": "0.0.18",
"version": "0.1.6",
"private": false,
"repository": "https://github.com/codecombat/codecombat-node",
"main": "./index.js",
"types": "./index.d.ts",
"scripts": {
"format": "prettier --write 'src/**/*.ts'",
"build": "tsc && tsc-alias",
"build": "tsc",
"prepack": "cp -rv dist/. ."
},
"dependencies": {
"@ungap/url-search-params": "0.2.2",
"url-join": "4.0.1",
"@types/url-join": "4.0.1",
"js-base64": "3.7.2",
"axios": "0.27.2"
"@ungap/url-search-params": "0.2.2",
"axios": "0.27.2",
"js-base64": "3.7.2"
},
"devDependencies": {
"@types/node": "17.0.33",
"prettier": "2.7.1",
"tsc-alias": "1.7.1",
"typescript": "4.6.4"
}
}
100 changes: 87 additions & 13 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

import * as environments from "./environments";
import * as core from "./core";
import * as CodeCombat from "./api";
import * as serializers from "./serialization";
import urlJoin from "url-join";
import * as errors from "./errors";
import { Auth } from "./api/resources/auth/client/Client";
import { Clans } from "./api/resources/clans/client/Client";
import { Classrooms } from "./api/resources/classrooms/client/Client";
Expand All @@ -12,41 +16,111 @@ import { Users } from "./api/resources/users/client/Client";

export declare namespace CodeCombatClient {
interface Options {
environment?: environments.CodeCombatEnvironment | string;
credentials: core.Supplier<core.BasicAuth>;
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
username: core.Supplier<string>;
password: core.Supplier<string>;
}

interface RequestOptions {
timeoutInSeconds?: number;
}
}

export class CodeCombatClient {
constructor(private readonly options: CodeCombatClient.Options) {}
constructor(protected readonly _options: CodeCombatClient.Options) {}

/**
* 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.
*
*/
public async postUsersHandleOAuthIdentities(
handle: string,
request: CodeCombat.PostUsersHandleOAuthIdentitiesRequest,
requestOptions?: CodeCombatClient.RequestOptions
): Promise<CodeCombat.UserResponse> {
const _response = await core.fetcher({
url: urlJoin(
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
`users/${handle}/o-auth-identities`
),
method: "POST",
headers: {
Authorization: await this._getAuthorizationHeader(),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "@fern-api/codecombat",
"X-Fern-SDK-Version": "0.1.6",
},
contentType: "application/json",
body: await serializers.PostUsersHandleOAuthIdentitiesRequest.jsonOrThrow(request, {
unrecognizedObjectKeys: "strip",
}),
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
});
if (_response.ok) {
return await serializers.UserResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}

if (_response.error.reason === "status-code") {
throw new errors.CodeCombatError({
statusCode: _response.error.statusCode,
body: _response.error.body,
});
}

private _auth: Auth | undefined;
switch (_response.error.reason) {
case "non-json":
throw new errors.CodeCombatError({
statusCode: _response.error.statusCode,
body: _response.error.rawBody,
});
case "timeout":
throw new errors.CodeCombatTimeoutError();
case "unknown":
throw new errors.CodeCombatError({
message: _response.error.errorMessage,
});
}
}

protected _auth: Auth | undefined;

public get auth(): Auth {
return (this._auth ??= new Auth(this.options));
return (this._auth ??= new Auth(this._options));
}

private _clans: Clans | undefined;
protected _clans: Clans | undefined;

public get clans(): Clans {
return (this._clans ??= new Clans(this.options));
return (this._clans ??= new Clans(this._options));
}

private _classrooms: Classrooms | undefined;
protected _classrooms: Classrooms | undefined;

public get classrooms(): Classrooms {
return (this._classrooms ??= new Classrooms(this.options));
return (this._classrooms ??= new Classrooms(this._options));
}

private _stats: Stats | undefined;
protected _stats: Stats | undefined;

public get stats(): Stats {
return (this._stats ??= new Stats(this.options));
return (this._stats ??= new Stats(this._options));
}

private _users: Users | undefined;
protected _users: Users | undefined;

public get users(): Users {
return (this._users ??= new Users(this.options));
return (this._users ??= new Users(this._options));
}

protected async _getAuthorizationHeader() {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
password: await core.Supplier.get(this._options.password),
});
}
}
1 change: 1 addition & 0 deletions src/api/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./requests";
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file was auto-generated by Fern from our API Definition.
*/

export interface AddOAuthIdentityRequest {
export interface PostUsersHandleOAuthIdentitiesRequest {
/** Your OAuth Provider ID. */
provider: string;
/** Will be passed through your lookup URL to get the user ID. Required if no `code`. */
Expand Down
1 change: 1 addition & 0 deletions src/api/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { PostUsersHandleOAuthIdentitiesRequest } from "./PostUsersHandleOAuthIdentitiesRequest";
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from "./types";
export * from "./resources";
export * from "./client";
42 changes: 26 additions & 16 deletions src/api/resources/auth/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,34 @@

import * as environments from "../../../../environments";
import * as core from "../../../../core";
import { CodeCombat } from "@fern-api/codecombat";
import URLSearchParams from "@ungap/url-search-params";
import * as CodeCombat from "../../..";
import { default as URLSearchParams } from "@ungap/url-search-params";
import urlJoin from "url-join";
import * as errors from "../../../../errors";

export declare namespace Auth {
interface Options {
environment?: environments.CodeCombatEnvironment | string;
credentials: core.Supplier<core.BasicAuth>;
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
username: core.Supplier<string>;
password: core.Supplier<string>;
}

interface RequestOptions {
timeoutInSeconds?: number;
}
}

export class Auth {
constructor(private readonly options: Auth.Options) {}
constructor(protected readonly _options: Auth.Options) {}

/**
* 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.
* 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.
*
*/
public async get(request: CodeCombat.GetUserAuthRequest): Promise<void> {
public async loginOauth(
request: CodeCombat.LoginOauthRequest,
requestOptions?: Auth.RequestOptions
): Promise<void> {
const { provider, accessToken, code, redirect, errorRedirect } = request;
const _queryParams = new URLSearchParams();
_queryParams.append("provider", provider);
Expand All @@ -45,15 +53,19 @@ export class Auth {

const _response = await core.fetcher({
url: urlJoin(
this.options.environment ?? environments.CodeCombatEnvironment.Production,
"/auth/login-o-auth"
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
"auth/login-o-auth"
),
method: "GET",
headers: {
Authorization: await this._getAuthorizationHeader(),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "@fern-api/codecombat",
"X-Fern-SDK-Version": "0.1.6",
},
contentType: "application/json",
queryParameters: _queryParams,
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
});
if (_response.ok) {
return;
Expand Down Expand Up @@ -81,12 +93,10 @@ export class Auth {
}
}

private async _getAuthorizationHeader() {
const credentials = await core.Supplier.get(this.options.credentials);
if (credentials != null) {
return core.BasicAuth.toAuthorizationHeader(await core.Supplier.get(credentials));
}

return undefined;
protected async _getAuthorizationHeader() {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
password: await core.Supplier.get(this._options.password),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file was auto-generated by Fern from our API Definition.
*/

export interface GetUserAuthRequest {
export interface LoginOauthRequest {
/**
* Your OAuth Provider ID
*/
Expand Down
2 changes: 1 addition & 1 deletion src/api/resources/auth/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { GetUserAuthRequest } from "./GetUserAuthRequest";
export { LoginOauthRequest } from "./LoginOauthRequest";
44 changes: 28 additions & 16 deletions src/api/resources/clans/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,56 @@

import * as environments from "../../../../environments";
import * as core from "../../../../core";
import { CodeCombat } from "@fern-api/codecombat";
import urlJoin from "url-join";
import * as CodeCombat from "../../..";
import * as serializers from "../../../../serialization";
import urlJoin from "url-join";
import * as errors from "../../../../errors";

export declare namespace Clans {
interface Options {
environment?: environments.CodeCombatEnvironment | string;
credentials: core.Supplier<core.BasicAuth>;
environment?: core.Supplier<environments.CodeCombatEnvironment | string>;
username: core.Supplier<string>;
password: core.Supplier<string>;
}

interface RequestOptions {
timeoutInSeconds?: number;
}
}

export class Clans {
constructor(private readonly options: Clans.Options) {}
constructor(protected readonly _options: Clans.Options) {}

/**
* Upserts a user into the clan.
*/
public async upsertClan(handle: string, request: CodeCombat.UpsertClanRequest): Promise<CodeCombat.ClanResponse> {
public async upsertMember(
handle: string,
request: CodeCombat.ClansUpsertMemberRequest,
requestOptions?: Clans.RequestOptions
): Promise<CodeCombat.ClanResponse> {
const _response = await core.fetcher({
url: urlJoin(
this.options.environment ?? environments.CodeCombatEnvironment.Production,
`/clan/${handle}/members`
(await core.Supplier.get(this._options.environment)) ?? environments.CodeCombatEnvironment.Default,
`clan/${handle}/members`
),
method: "PUT",
headers: {
Authorization: await this._getAuthorizationHeader(),
"X-Fern-Language": "JavaScript",
"X-Fern-SDK-Name": "@fern-api/codecombat",
"X-Fern-SDK-Version": "0.1.6",
},
contentType: "application/json",
body: await serializers.UpsertClanRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
body: await serializers.ClansUpsertMemberRequest.jsonOrThrow(request, { unrecognizedObjectKeys: "strip" }),
timeoutMs: requestOptions?.timeoutInSeconds != null ? requestOptions.timeoutInSeconds * 1000 : 60000,
});
if (_response.ok) {
return await serializers.ClanResponse.parseOrThrow(_response.body, {
unrecognizedObjectKeys: "passthrough",
allowUnrecognizedUnionMembers: true,
allowUnrecognizedEnumValues: true,
breadcrumbsPrefix: ["response"],
});
}

Expand All @@ -65,12 +79,10 @@ export class Clans {
}
}

private async _getAuthorizationHeader() {
const credentials = await core.Supplier.get(this.options.credentials);
if (credentials != null) {
return core.BasicAuth.toAuthorizationHeader(await core.Supplier.get(credentials));
}

return undefined;
protected async _getAuthorizationHeader() {
return core.BasicAuth.toAuthorizationHeader({
username: await core.Supplier.get(this._options.username),
password: await core.Supplier.get(this._options.password),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This file was auto-generated by Fern from our API Definition.
*/

export interface UpsertClanRequest {
export interface ClansUpsertMemberRequest {
/** The `_id` or `slug` of the user to add to the clan. */
userId: string;
}
2 changes: 1 addition & 1 deletion src/api/resources/clans/client/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { UpsertClanRequest } from "./UpsertClanRequest";
export { ClansUpsertMemberRequest } from "./ClansUpsertMemberRequest";
1 change: 0 additions & 1 deletion src/api/resources/clans/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./types";
export * from "./client";
1 change: 0 additions & 1 deletion src/api/resources/clans/types/index.ts

This file was deleted.

Loading